Skip to content

Commit d334027

Browse files
committed
Auto merge of #52230 - alexcrichton:attr-and-derive, r=petrochenkov
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
2 parents de857bb + 743a817 commit d334027

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

Diff for: src/librustc_resolve/macros.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,22 @@ impl<'a> Resolver<'a> {
385385
Err(Determinacy::Determined) => {}
386386
}
387387

388+
// Ok at this point we've determined that the `attr` above doesn't
389+
// actually resolve at this time, so we may want to report an error.
390+
// It could be the case, though, that `attr` won't ever resolve! If
391+
// there's a custom derive that could be used it might declare `attr` as
392+
// a custom attribute accepted by the derive. In this case we don't want
393+
// to report this particular invocation as unresolved, but rather we'd
394+
// want to move on to the next invocation.
395+
//
396+
// This loop here looks through all of the derive annotations in scope
397+
// and tries to resolve them. If they themselves successfully resolve
398+
// *and* the resolve mentions that this attribute's name is a registered
399+
// custom attribute then we flag this attribute as known and update
400+
// `invoc` above to point to the next invocation.
401+
//
402+
// By then returning `Undetermined` we should continue resolution to
403+
// resolve the next attribute.
388404
let attr_name = match path.segments.len() {
389405
1 => path.segments[0].ident.name,
390406
_ => return Err(determinacy),
@@ -406,8 +422,8 @@ impl<'a> Resolver<'a> {
406422
attrs.push(inert_attr);
407423
attrs
408424
});
425+
return Err(Determinacy::Undetermined)
409426
}
410-
return Err(Determinacy::Undetermined);
411427
},
412428
Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
413429
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)