Skip to content

Commit 743a817

Browse files
committed
rustc: Search all derives for inert attributes
This commit fixes an apparent mistake in librustc_resolve where when the `proc_macro` feature is enabled (or `rust_2018_preview`) the resolution of custom attributes for custom derive was tweaked. Previously when an attribute failed to resolve it was attempted to locate if there is a custom derive also in scope which declares the attribute, but only the first custom derive directive was search. Instead this commit fixes the loop to search all custom derive invocations looking for any which register the attribute in question. Closes #52219
1 parent 90bd83c commit 743a817

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/librustc_resolve/macros.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,22 @@ impl<'a> Resolver<'a> {
390390
Err(Determinacy::Determined) => {}
391391
}
392392

393+
// Ok at this point we've determined that the `attr` above doesn't
394+
// actually resolve at this time, so we may want to report an error.
395+
// It could be the case, though, that `attr` won't ever resolve! If
396+
// there's a custom derive that could be used it might declare `attr` as
397+
// a custom attribute accepted by the derive. In this case we don't want
398+
// to report this particular invocation as unresolved, but rather we'd
399+
// want to move on to the next invocation.
400+
//
401+
// This loop here looks through all of the derive annotations in scope
402+
// and tries to resolve them. If they themselves successfully resolve
403+
// *and* the resolve mentions that this attribute's name is a registered
404+
// custom attribute then we flag this attribute as known and update
405+
// `invoc` above to point to the next invocation.
406+
//
407+
// By then returning `Undetermined` we should continue resolution to
408+
// resolve the next attribute.
393409
let attr_name = match path.segments.len() {
394410
1 => path.segments[0].ident.name,
395411
_ => return Err(determinacy),
@@ -411,8 +427,8 @@ impl<'a> Resolver<'a> {
411427
attrs.push(inert_attr);
412428
attrs
413429
});
430+
return Err(Determinacy::Undetermined)
414431
}
415-
return Err(Determinacy::Undetermined);
416432
},
417433
Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
418434
Err(Determinacy::Determined) => {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// no-prefer-dynamic
12+
13+
#![crate_type = "proc-macro"]
14+
15+
extern crate proc_macro;
16+
17+
use proc_macro::TokenStream;
18+
19+
#[proc_macro_derive(Foo)]
20+
pub fn foo(a: TokenStream) -> TokenStream {
21+
"".parse().unwrap()
22+
}
23+
24+
#[proc_macro_derive(Bar, attributes(custom))]
25+
pub fn bar(a: TokenStream) -> TokenStream {
26+
"".parse().unwrap()
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:custom-attr-only-one-derive.rs
12+
13+
#![feature(rust_2018_preview)]
14+
15+
#[macro_use]
16+
extern crate custom_attr_only_one_derive;
17+
18+
#[derive(Bar, Foo)]
19+
#[custom = "test"]
20+
pub enum A {
21+
B,
22+
C,
23+
}
24+
25+
fn main() {}

0 commit comments

Comments
 (0)