Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 79f0d88

Browse files
committed
resolve: Use feature(custom_attribute) fallback only if the feature is enabled
Normally `#![feature(...)]` shouldn't change behavior, but custom attributes in particular are in the process of retirement, and we should not produce a message telling to enable them. It also helps with unifying diagnostics for unresolved macros.
1 parent 1e1b081 commit 79f0d88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+144
-379
lines changed

src/librustc_resolve/diagnostics.rs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc::ty::{self, DefIdTree};
1111
use rustc::util::nodemap::FxHashSet;
1212
use syntax::ast::{self, Expr, ExprKind, Ident, NodeId, Path, Ty, TyKind};
1313
use syntax::ext::base::MacroKind;
14-
use syntax::feature_gate::{feature_err, AttributeGate, GateIssue, Stability, BUILTIN_ATTRIBUTES};
1514
use syntax::symbol::{Symbol, kw};
1615
use syntax::util::lev_distance::find_best_match_for_name;
1716
use syntax_pos::{BytePos, Span};
@@ -903,60 +902,6 @@ impl<'a> Resolver<'a> {
903902
}
904903
}
905904
}
906-
907-
crate fn report_unknown_attribute(&self, span: Span, name: &str, msg: &str, feature: Symbol) {
908-
let mut err = feature_err(
909-
&self.session.parse_sess,
910-
feature,
911-
span,
912-
GateIssue::Language,
913-
&msg,
914-
);
915-
916-
let features = self.session.features_untracked();
917-
918-
let attr_candidates = BUILTIN_ATTRIBUTES
919-
.iter()
920-
.filter_map(|&(name, _, _, ref gate)| {
921-
if name.as_str().starts_with("rustc_") && !features.rustc_attrs {
922-
return None;
923-
}
924-
925-
match gate {
926-
AttributeGate::Gated(Stability::Unstable, ..)
927-
if self.session.opts.unstable_features.is_nightly_build() =>
928-
{
929-
Some(name)
930-
}
931-
AttributeGate::Gated(Stability::Deprecated(..), ..) => Some(name),
932-
AttributeGate::Ungated => Some(name),
933-
_ => None,
934-
}
935-
})
936-
.chain(
937-
// Add built-in macro attributes as well.
938-
self.builtin_macros.iter().filter_map(|(name, binding)| {
939-
match binding.macro_kind() {
940-
Some(MacroKind::Attr) => Some(*name),
941-
_ => None,
942-
}
943-
}),
944-
)
945-
.collect::<Vec<_>>();
946-
947-
let lev_suggestion = find_best_match_for_name(attr_candidates.iter(), &name, None);
948-
949-
if let Some(suggestion) = lev_suggestion {
950-
err.span_suggestion(
951-
span,
952-
"a built-in attribute with a similar name exists",
953-
suggestion.to_string(),
954-
Applicability::MaybeIncorrect,
955-
);
956-
}
957-
958-
err.emit();
959-
}
960905
}
961906

962907
impl<'a, 'b> ImportResolver<'a, 'b> {

src/librustc_resolve/macros.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -312,23 +312,7 @@ impl<'a> Resolver<'a> {
312312
}
313313
}
314314
}
315-
Res::NonMacroAttr(attr_kind) => {
316-
if attr_kind == NonMacroAttrKind::Custom {
317-
assert!(path.segments.len() == 1);
318-
if !features.custom_attribute {
319-
let msg = format!("The attribute `{}` is currently unknown to the \
320-
compiler and may have meaning added to it in the \
321-
future", path);
322-
self.report_unknown_attribute(
323-
path.span,
324-
&path.segments[0].ident.as_str(),
325-
&msg,
326-
sym::custom_attribute,
327-
);
328-
}
329-
}
330-
}
331-
Res::Err => {}
315+
Res::NonMacroAttr(..) | Res::Err => {}
332316
_ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
333317
};
334318

@@ -721,7 +705,8 @@ impl<'a> Resolver<'a> {
721705
}
722706

723707
let determinacy = Determinacy::determined(determinacy == Determinacy::Determined || force);
724-
if determinacy == Determinacy::Determined && macro_kind == Some(MacroKind::Attr) {
708+
if determinacy == Determinacy::Determined && macro_kind == Some(MacroKind::Attr) &&
709+
self.session.features_untracked().custom_attribute {
725710
// For single-segment attributes interpret determinate "no resolution" as a custom
726711
// attribute. (Lexical resolution implies the first segment and attr kind should imply
727712
// the last segment, so we are certainly working with a single-segment attribute here.)
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Obsolete attributes fall back to feature gated custom attributes.
22

3-
#[ab_isize="stdcall"] extern {} //~ ERROR attribute `ab_isize` is currently unknown
3+
#[ab_isize="stdcall"] extern {}
4+
//~^ ERROR cannot find attribute macro `ab_isize` in this scope
45

5-
#[fixed_stack_segment] fn f() {} //~ ERROR attribute `fixed_stack_segment` is currently unknown
6+
#[fixed_stack_segment] fn f() {}
7+
//~^ ERROR cannot find attribute macro `fixed_stack_segment` in this scope
68

79
fn main() {}
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
error[E0658]: The attribute `fixed_stack_segment` is currently unknown to the compiler and may have meaning added to it in the future
2-
--> $DIR/obsolete-attr.rs:5:3
1+
error: cannot find attribute macro `fixed_stack_segment` in this scope
2+
--> $DIR/obsolete-attr.rs:6:3
33
|
44
LL | #[fixed_stack_segment] fn f() {}
55
| ^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
8-
= help: add `#![feature(custom_attribute)]` to the crate attributes to enable
96

10-
error[E0658]: The attribute `ab_isize` is currently unknown to the compiler and may have meaning added to it in the future
7+
error: cannot find attribute macro `ab_isize` in this scope
118
--> $DIR/obsolete-attr.rs:3:3
129
|
1310
LL | #[ab_isize="stdcall"] extern {}
1411
| ^^^^^^^^
15-
|
16-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
17-
= help: add `#![feature(custom_attribute)]` to the crate attributes to enable
1812

1913
error: aborting due to 2 previous errors
2014

21-
For more information about this error, try `rustc --explain E0658`.

src/test/ui/attributes/unknown-attr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
#![feature(custom_inner_attributes)]
44

5-
#![mutable_doc] //~ ERROR attribute `mutable_doc` is currently unknown
5+
#![mutable_doc]
6+
//~^ ERROR cannot find attribute macro `mutable_doc` in this scope
67

7-
#[dance] mod a {} //~ ERROR attribute `dance` is currently unknown
8+
#[dance] mod a {}
9+
//~^ ERROR cannot find attribute macro `dance` in this scope
810

9-
#[dance] fn main() {} //~ ERROR attribute `dance` is currently unknown
11+
#[dance] fn main() {}
12+
//~^ ERROR cannot find attribute macro `dance` in this scope
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
error[E0658]: The attribute `mutable_doc` is currently unknown to the compiler and may have meaning added to it in the future
1+
error: cannot find attribute macro `mutable_doc` in this scope
22
--> $DIR/unknown-attr.rs:5:4
33
|
44
LL | #![mutable_doc]
55
| ^^^^^^^^^^^
6-
|
7-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
8-
= help: add `#![feature(custom_attribute)]` to the crate attributes to enable
96

10-
error[E0658]: The attribute `dance` is currently unknown to the compiler and may have meaning added to it in the future
11-
--> $DIR/unknown-attr.rs:7:3
7+
error: cannot find attribute macro `dance` in this scope
8+
--> $DIR/unknown-attr.rs:8:3
129
|
1310
LL | #[dance] mod a {}
1411
| ^^^^^
15-
|
16-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
17-
= help: add `#![feature(custom_attribute)]` to the crate attributes to enable
1812

19-
error[E0658]: The attribute `dance` is currently unknown to the compiler and may have meaning added to it in the future
20-
--> $DIR/unknown-attr.rs:9:3
13+
error: cannot find attribute macro `dance` in this scope
14+
--> $DIR/unknown-attr.rs:11:3
2115
|
2216
LL | #[dance] fn main() {}
2317
| ^^^^^
24-
|
25-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
26-
= help: add `#![feature(custom_attribute)]` to the crate attributes to enable
2718

2819
error: aborting due to 3 previous errors
2920

30-
For more information about this error, try `rustc --explain E0658`.

src/test/ui/conditional-compilation/cfg-attr-unknown-attribute-macro-expansion.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
macro_rules! foo {
22
() => {
3-
#[cfg_attr(all(), unknown)] //~ ERROR `unknown` is currently unknown
3+
#[cfg_attr(all(), unknown)]
4+
//~^ ERROR cannot find attribute macro `unknown` in this scope
45
fn foo() {}
56
}
67
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
error[E0658]: The attribute `unknown` is currently unknown to the compiler and may have meaning added to it in the future
1+
error: cannot find attribute macro `unknown` in this scope
22
--> $DIR/cfg-attr-unknown-attribute-macro-expansion.rs:3:27
33
|
44
LL | #[cfg_attr(all(), unknown)]
55
| ^^^^^^^
66
...
77
LL | foo!();
88
| ------- in this macro invocation
9-
|
10-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
11-
= help: add `#![feature(custom_attribute)]` to the crate attributes to enable
129

1310
error: aborting due to previous error
1411

15-
For more information about this error, try `rustc --explain E0658`.

src/test/ui/custom_attribute.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![feature(stmt_expr_attributes)]
22

3-
#[foo] //~ ERROR The attribute `foo`
3+
#[foo] //~ ERROR cannot find attribute macro `foo` in this scope
44
fn main() {
5-
#[foo] //~ ERROR The attribute `foo`
5+
#[foo] //~ ERROR cannot find attribute macro `foo` in this scope
66
let x = ();
7-
#[foo] //~ ERROR The attribute `foo`
7+
#[foo] //~ ERROR cannot find attribute macro `foo` in this scope
88
x
99
}

src/test/ui/custom_attribute.stderr

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
error[E0658]: The attribute `foo` is currently unknown to the compiler and may have meaning added to it in the future
1+
error: cannot find attribute macro `foo` in this scope
22
--> $DIR/custom_attribute.rs:3:3
33
|
44
LL | #[foo]
55
| ^^^
6-
|
7-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
8-
= help: add `#![feature(custom_attribute)]` to the crate attributes to enable
96

10-
error[E0658]: The attribute `foo` is currently unknown to the compiler and may have meaning added to it in the future
7+
error: cannot find attribute macro `foo` in this scope
118
--> $DIR/custom_attribute.rs:5:7
129
|
1310
LL | #[foo]
1411
| ^^^
15-
|
16-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
17-
= help: add `#![feature(custom_attribute)]` to the crate attributes to enable
1812

19-
error[E0658]: The attribute `foo` is currently unknown to the compiler and may have meaning added to it in the future
13+
error: cannot find attribute macro `foo` in this scope
2014
--> $DIR/custom_attribute.rs:7:7
2115
|
2216
LL | #[foo]
2317
| ^^^
24-
|
25-
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
26-
= help: add `#![feature(custom_attribute)]` to the crate attributes to enable
2718

2819
error: aborting due to 3 previous errors
2920

30-
For more information about this error, try `rustc --explain E0658`.
Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
// Check that literals in attributes parse just fine.
22

3+
#[fake_attr] //~ ERROR cannot find attribute macro `fake_attr` in this scope
4+
#[fake_attr(100)] //~ ERROR cannot find attribute macro `fake_attr` in this scope
5+
#[fake_attr(1, 2, 3)] //~ ERROR cannot find attribute macro `fake_attr` in this scope
6+
#[fake_attr("hello")] //~ ERROR cannot find attribute macro `fake_attr` in this scope
7+
#[fake_attr(name = "hello")] //~ ERROR cannot find attribute macro `fake_attr` in this scope
8+
#[fake_attr(1, "hi", key = 12, true, false)] //~ ERROR cannot find attribute macro `fake_attr` in th
9+
#[fake_attr(key = "hello", val = 10)] //~ ERROR cannot find attribute macro `fake_attr` in this scop
10+
#[fake_attr(key("hello"), val(10))] //~ ERROR cannot find attribute macro `fake_attr` in this scope
11+
#[fake_attr(enabled = true, disabled = false)] //~ ERROR cannot find attribute macro `fake_attr` in
12+
#[fake_attr(true)] //~ ERROR cannot find attribute macro `fake_attr` in this scope
13+
#[fake_attr(pi = 3.14159)] //~ ERROR cannot find attribute macro `fake_attr` in this scope
14+
#[fake_attr(b"hi")] //~ ERROR cannot find attribute macro `fake_attr` in this scope
15+
#[fake_doc(r"doc")] //~ ERROR cannot find attribute macro `fake_doc` in this scope
16+
struct Q {}
317

4-
#![allow(dead_code)]
5-
#![allow(unused_variables)]
6-
7-
#[fake_attr] //~ ERROR attribute `fake_attr` is currently unknown
8-
#[fake_attr(100)] //~ ERROR attribute `fake_attr` is currently unknown
9-
#[fake_attr(1, 2, 3)] //~ ERROR attribute `fake_attr` is currently unknown
10-
#[fake_attr("hello")] //~ ERROR attribute `fake_attr` is currently unknown
11-
#[fake_attr(name = "hello")] //~ ERROR attribute `fake_attr` is currently unknown
12-
#[fake_attr(1, "hi", key = 12, true, false)] //~ ERROR attribute `fake_attr` is currently unknown
13-
#[fake_attr(key = "hello", val = 10)] //~ ERROR attribute `fake_attr` is currently unknown
14-
#[fake_attr(key("hello"), val(10))] //~ ERROR attribute `fake_attr` is currently unknown
15-
#[fake_attr(enabled = true, disabled = false)] //~ ERROR attribute `fake_attr` is currently unknown
16-
#[fake_attr(true)] //~ ERROR attribute `fake_attr` is currently unknown
17-
#[fake_attr(pi = 3.14159)] //~ ERROR attribute `fake_attr` is currently unknown
18-
#[fake_attr(b"hi")] //~ ERROR attribute `fake_attr` is currently unknown
19-
#[fake_doc(r"doc")] //~ ERROR attribute `fake_doc` is currently unknown
20-
struct Q { }
21-
22-
23-
fn main() { }
18+
fn main() {}

0 commit comments

Comments
 (0)