Skip to content

Commit d5a448b

Browse files
committed
Auto merge of #53270 - petrochenkov:macuse-regr, r=alexcrichton
Fix a few regressions from enabling macro modularization The first commit restores the old behavior for some minor unstable stuff (`rustc_*` and `derive_*` attributes) and adds a new feature gate for arbitrary tokens in non-macro attributes. The second commit fixes #53205 The third commit fixes #53144. Same technique is used as for other things blocking expansion progress - if something causes indeterminacy too often, then prohibit it. In this case referring to crate-local macro-expanded `#[macro_export]` macros via module-relative paths is prohibited, see comments in code for more details. cc #50911
2 parents a78ae85 + dd0a766 commit d5a448b

24 files changed

+262
-116
lines changed

src/librustc_resolve/build_reduced_graph.rs

-1
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,6 @@ impl<'a, 'b, 'cl> BuildReducedGraphVisitor<'a, 'b, 'cl> {
789789
fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
790790
let mark = id.placeholder_to_mark();
791791
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
792-
self.resolver.unresolved_invocations_macro_export.insert(mark);
793792
let invocation = self.resolver.invocations[&mark];
794793
invocation.module.set(self.resolver.current_module);
795794
invocation.legacy_scope.set(self.legacy_scope);

src/librustc_resolve/lib.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,8 @@ pub struct Resolver<'a, 'b: 'a> {
13851385
use_injections: Vec<UseError<'a>>,
13861386
/// `use` injections for proc macros wrongly imported with #[macro_use]
13871387
proc_mac_errors: Vec<macros::ProcMacError>,
1388+
/// crate-local macro expanded `macro_export` referred to by a module-relative path
1389+
macro_expanded_macro_export_errors: BTreeSet<(Span, Span)>,
13881390

13891391
gated_errors: FxHashSet<Span>,
13901392
disallowed_shadowing: Vec<&'a LegacyBinding<'a>>,
@@ -1432,9 +1434,6 @@ pub struct Resolver<'a, 'b: 'a> {
14321434

14331435
/// Only supposed to be used by rustdoc, otherwise should be false.
14341436
pub ignore_extern_prelude_feature: bool,
1435-
1436-
/// Macro invocations in the whole crate that can expand into a `#[macro_export] macro_rules`.
1437-
unresolved_invocations_macro_export: FxHashSet<Mark>,
14381437
}
14391438

14401439
/// Nothing really interesting here, it just provides memory for the rest of the crate.
@@ -1706,6 +1705,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
17061705
proc_mac_errors: Vec::new(),
17071706
gated_errors: FxHashSet(),
17081707
disallowed_shadowing: Vec::new(),
1708+
macro_expanded_macro_export_errors: BTreeSet::new(),
17091709

17101710
arenas,
17111711
dummy_binding: arenas.alloc_name_binding(NameBinding {
@@ -1737,7 +1737,6 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
17371737
current_type_ascription: Vec::new(),
17381738
injected_crate: None,
17391739
ignore_extern_prelude_feature: false,
1740-
unresolved_invocations_macro_export: FxHashSet(),
17411740
}
17421741
}
17431742

@@ -4126,6 +4125,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
41264125
ns: Namespace,
41274126
module: Module<'a>,
41284127
found_traits: &mut Vec<TraitCandidate>) {
4128+
assert!(ns == TypeNS || ns == ValueNS);
41294129
let mut traits = module.traits.borrow_mut();
41304130
if traits.is_none() {
41314131
let mut collected_traits = Vec::new();
@@ -4371,6 +4371,14 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
43714371
self.report_proc_macro_import(krate);
43724372
let mut reported_spans = FxHashSet();
43734373

4374+
for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {
4375+
let msg = "macro-expanded `macro_export` macros from the current crate \
4376+
cannot be referred to by absolute paths";
4377+
self.session.struct_span_err(span_use, msg)
4378+
.span_note(span_def, "the macro is defined here")
4379+
.emit();
4380+
}
4381+
43744382
for &AmbiguityError { span, name, b1, b2, lexical } in &self.ambiguity_errors {
43754383
if !reported_spans.insert(span) { continue }
43764384
let participle = |binding: &NameBinding| {

src/librustc_resolve/macros.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
2828
use syntax::ext::hygiene::{self, Mark};
2929
use syntax::ext::tt::macro_rules;
3030
use syntax::feature_gate::{self, feature_err, emit_feature_err, is_builtin_attr_name, GateIssue};
31+
use syntax::feature_gate::EXPLAIN_DERIVE_UNDERSCORE;
3132
use syntax::fold::{self, Folder};
3233
use syntax::parse::parser::PathStyle;
3334
use syntax::parse::token::{self, Token};
@@ -195,9 +196,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
195196

196197
self.current_module = invocation.module.get();
197198
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
198-
self.unresolved_invocations_macro_export.remove(&mark);
199199
self.current_module.unresolved_invocations.borrow_mut().extend(derives);
200-
self.unresolved_invocations_macro_export.extend(derives);
201200
for &derive in derives {
202201
self.invocations.insert(derive, invocation);
203202
}
@@ -338,19 +337,37 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
338337
match attr_kind {
339338
NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper |
340339
NonMacroAttrKind::Custom if is_attr_invoc => {
340+
let features = self.session.features_untracked();
341341
if attr_kind == NonMacroAttrKind::Tool &&
342-
!self.session.features_untracked().tool_attributes {
342+
!features.tool_attributes {
343343
feature_err(&self.session.parse_sess, "tool_attributes",
344344
invoc.span(), GateIssue::Language,
345345
"tool attributes are unstable").emit();
346346
}
347-
if attr_kind == NonMacroAttrKind::Custom &&
348-
!self.session.features_untracked().custom_attribute {
349-
let msg = format!("The attribute `{}` is currently unknown to the compiler \
350-
and may have meaning added to it in the future", path);
351-
feature_err(&self.session.parse_sess, "custom_attribute", invoc.span(),
352-
GateIssue::Language, &msg).emit();
347+
if attr_kind == NonMacroAttrKind::Custom {
348+
assert!(path.segments.len() == 1);
349+
let name = path.segments[0].ident.name.as_str();
350+
if name.starts_with("rustc_") {
351+
if !features.rustc_attrs {
352+
let msg = "unless otherwise specified, attributes with the prefix \
353+
`rustc_` are reserved for internal compiler diagnostics";
354+
feature_err(&self.session.parse_sess, "rustc_attrs", invoc.span(),
355+
GateIssue::Language, &msg).emit();
356+
}
357+
} else if name.starts_with("derive_") {
358+
if !features.custom_derive {
359+
feature_err(&self.session.parse_sess, "custom_derive", invoc.span(),
360+
GateIssue::Language, EXPLAIN_DERIVE_UNDERSCORE).emit();
361+
}
362+
} else if !features.custom_attribute {
363+
let msg = format!("The attribute `{}` is currently unknown to the \
364+
compiler and may have meaning added to it in the \
365+
future", path);
366+
feature_err(&self.session.parse_sess, "custom_attribute", invoc.span(),
367+
GateIssue::Language, &msg).emit();
368+
}
353369
}
370+
354371
return Ok(Some(Lrc::new(SyntaxExtension::NonMacroAttr {
355372
mark_used: attr_kind == NonMacroAttrKind::Tool,
356373
})));
@@ -650,7 +667,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
650667
}
651668
}
652669
WhereToResolve::BuiltinAttrs => {
653-
if is_builtin_attr_name(ident.name) {
670+
// FIXME: Only built-in attributes are not considered as candidates for
671+
// non-attributes to fight off regressions on stable channel (#53205).
672+
// We need to come up with some more principled approach instead.
673+
if is_attr && is_builtin_attr_name(ident.name) {
654674
let binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
655675
ty::Visibility::Public, ident.span, Mark::root())
656676
.to_name_binding(self.arenas);

src/librustc_resolve/resolve_imports.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
146146
.try_borrow_mut()
147147
.map_err(|_| Determined)?; // This happens when there is a cycle of imports
148148

149+
if let Some(binding) = resolution.binding {
150+
if !restricted_shadowing && binding.expansion != Mark::root() {
151+
if let NameBindingKind::Def(_, true) = binding.kind {
152+
self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
153+
}
154+
}
155+
}
156+
149157
if record_used {
150158
if let Some(binding) = resolution.binding {
151159
if let Some(shadowed_glob) = resolution.shadowed_glob {
@@ -211,9 +219,15 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
211219
// if it cannot be shadowed by some new item/import expanded from a macro.
212220
// This happens either if there are no unexpanded macros, or expanded names cannot
213221
// shadow globs (that happens in macro namespace or with restricted shadowing).
214-
let unexpanded_macros = !module.unresolved_invocations.borrow().is_empty() ||
215-
(ns == MacroNS && ptr::eq(module, self.graph_root) &&
216-
!self.unresolved_invocations_macro_export.is_empty());
222+
//
223+
// Additionally, any macro in any module can plant names in the root module if it creates
224+
// `macro_export` macros, so the root module effectively has unresolved invocations if any
225+
// module has unresolved invocations.
226+
// However, it causes resolution/expansion to stuck too often (#53144), so, to make
227+
// progress, we have to ignore those potential unresolved invocations from other modules
228+
// and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
229+
// shadowing is enabled, see `macro_expanded_macro_export_errors`).
230+
let unexpanded_macros = !module.unresolved_invocations.borrow().is_empty();
217231
if let Some(binding) = resolution.binding {
218232
if !unexpanded_macros || ns == MacroNS || restricted_shadowing {
219233
return check_usable(self, binding);

src/libsyntax/feature_gate.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ macro_rules! declare_features {
9090
self.macros_in_extern || self.proc_macro_path_invoc ||
9191
self.proc_macro_mod || self.proc_macro_expr ||
9292
self.proc_macro_non_items || self.proc_macro_gen ||
93-
self.stmt_expr_attributes
93+
self.stmt_expr_attributes || self.unrestricted_attribute_tokens
9494
}
9595
}
9696
};
@@ -504,6 +504,9 @@ declare_features! (
504504
// impl<I:Iterator> Iterator for &mut Iterator
505505
// impl Debug for Foo<'_>
506506
(active, impl_header_lifetime_elision, "1.30.0", Some(15872), Some(Edition::Edition2018)),
507+
508+
// Support for arbitrary delimited token streams in non-macro attributes.
509+
(active, unrestricted_attribute_tokens, "1.30.0", Some(44690), None),
507510
);
508511

509512
declare_features! (
@@ -721,8 +724,7 @@ pub fn is_builtin_attr_name(name: ast::Name) -> bool {
721724
}
722725

723726
pub fn is_builtin_attr(attr: &ast::Attribute) -> bool {
724-
BUILTIN_ATTRIBUTES.iter().any(|&(builtin_name, _, _)| attr.path == builtin_name) ||
725-
attr.name().as_str().starts_with("rustc_")
727+
BUILTIN_ATTRIBUTES.iter().any(|&(builtin_name, _, _)| attr.path == builtin_name)
726728
}
727729

728730
// Attributes that have a special meaning to rustc or rustdoc
@@ -1521,25 +1523,27 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
15211523
}
15221524
}
15231525

1524-
// allow attr_literals in #[repr(align(x))] and #[repr(packed(n))]
1525-
let mut allow_attr_literal = false;
1526-
if attr.path == "repr" {
1527-
if let Some(content) = attr.meta_item_list() {
1528-
allow_attr_literal = content.iter().any(
1529-
|c| c.check_name("align") || c.check_name("packed"));
1530-
}
1531-
}
1532-
1533-
if self.context.features.use_extern_macros() && attr::is_known(attr) {
1534-
return
1535-
}
1526+
match attr.parse_meta(self.context.parse_sess) {
1527+
Ok(meta) => {
1528+
// allow attr_literals in #[repr(align(x))] and #[repr(packed(n))]
1529+
let mut allow_attr_literal = false;
1530+
if attr.path == "repr" {
1531+
if let Some(content) = meta.meta_item_list() {
1532+
allow_attr_literal = content.iter().any(
1533+
|c| c.check_name("align") || c.check_name("packed"));
1534+
}
1535+
}
15361536

1537-
if !allow_attr_literal {
1538-
let meta = panictry!(attr.parse_meta(self.context.parse_sess));
1539-
if contains_novel_literal(&meta) {
1540-
gate_feature_post!(&self, attr_literals, attr.span,
1541-
"non-string literals in attributes, or string \
1542-
literals in top-level positions, are experimental");
1537+
if !allow_attr_literal && contains_novel_literal(&meta) {
1538+
gate_feature_post!(&self, attr_literals, attr.span,
1539+
"non-string literals in attributes, or string \
1540+
literals in top-level positions, are experimental");
1541+
}
1542+
}
1543+
Err(mut err) => {
1544+
err.cancel();
1545+
gate_feature_post!(&self, unrestricted_attribute_tokens, attr.span,
1546+
"arbitrary tokens in non-macro attributes are unstable");
15431547
}
15441548
}
15451549
}

src/test/compile-fail-fulldeps/proc-macro/proc-macro-attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern crate derive_b;
2121
#[C] //~ ERROR: The attribute `C` is currently unknown to the compiler
2222
#[B(D)]
2323
#[B(E = "foo")]
24-
#[B arbitrary tokens] //~ expected one of `(` or `=`, found `arbitrary`
24+
#[B arbitrary tokens] //~ ERROR arbitrary tokens in non-macro attributes are unstable
2525
struct B;
2626

2727
fn main() {}

src/test/compile-fail/gated-attr-literals.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,33 @@
1111
// Check that literals in attributes don't parse without the feature gate.
1212

1313
// gate-test-attr_literals
14-
// gate-test-custom_attribute
1514

16-
#![feature(rustc_attrs)]
17-
#![allow(dead_code)]
18-
#![allow(unused_variables)]
15+
#![feature(custom_attribute)]
1916

20-
#[fake_attr] //~ ERROR attribute `fake_attr` is currently unknown
21-
#[fake_attr(100)] //~ ERROR attribute `fake_attr` is currently unknown
17+
#[fake_attr] // OK
18+
#[fake_attr(100)]
2219
//~^ ERROR non-string literals in attributes
23-
#[fake_attr(1, 2, 3)] //~ ERROR attribute `fake_attr` is currently unknown
20+
#[fake_attr(1, 2, 3)]
2421
//~^ ERROR non-string literals in attributes
25-
#[fake_attr("hello")] //~ ERROR attribute `fake_attr` is currently unknown
22+
#[fake_attr("hello")]
2623
//~^ ERROR string literals in top-level positions, are experimental
27-
#[fake_attr(name = "hello")] //~ ERROR attribute `fake_attr` is currently unknown
28-
#[fake_attr(1, "hi", key = 12, true, false)] //~ ERROR attribute `fake_attr` is currently unknown
24+
#[fake_attr(name = "hello")] // OK
25+
#[fake_attr(1, "hi", key = 12, true, false)]
2926
//~^ ERROR non-string literals in attributes, or string literals in top-level positions
30-
#[fake_attr(key = "hello", val = 10)] //~ ERROR attribute `fake_attr` is currently unknown
27+
#[fake_attr(key = "hello", val = 10)]
3128
//~^ ERROR non-string literals in attributes
32-
#[fake_attr(key("hello"), val(10))] //~ ERROR attribute `fake_attr` is currently unknown
29+
#[fake_attr(key("hello"), val(10))]
3330
//~^ ERROR non-string literals in attributes, or string literals in top-level positions
34-
#[fake_attr(enabled = true, disabled = false)] //~ ERROR attribute `fake_attr` is currently unknown
31+
#[fake_attr(enabled = true, disabled = false)]
3532
//~^ ERROR non-string literals in attributes
36-
#[fake_attr(true)] //~ ERROR attribute `fake_attr` is currently unknown
33+
#[fake_attr(true)]
3734
//~^ ERROR non-string literals in attributes
38-
#[fake_attr(pi = 3.14159)] //~ ERROR attribute `fake_attr` is currently unknown
35+
#[fake_attr(pi = 3.14159)]
3936
//~^ ERROR non-string literals in attributes
40-
#[fake_attr(b"hi")] //~ ERROR attribute `fake_attr` is currently unknown
37+
#[fake_attr(b"hi")]
4138
//~^ ERROR string literals in top-level positions, are experimental
42-
#[fake_doc(r"doc")] //~ ERROR attribute `fake_doc` is currently unknown
39+
#[fake_doc(r"doc")]
4340
//~^ ERROR string literals in top-level positions, are experimental
4441
struct Q { }
4542

46-
#[rustc_error]
4743
fn main() { }

src/test/compile-fail/macro-attribute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[doc = $not_there] //~ error: unexpected token: `$`
11+
#[doc = $not_there] //~ ERROR arbitrary tokens in non-macro attributes are unstable
1212
fn main() { }

src/test/parse-fail/attr-bad-meta.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
// except according to those terms.
1010

1111
// asterisk is bogus
12-
#[path*] //~ ERROR expected one of `(` or `=`
12+
#[path*] //~ ERROR arbitrary tokens in non-macro attributes are unstable
1313
mod m {}

src/test/run-pass-fulldeps/proc-macro/derive-b.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// aux-build:derive-b.rs
1212
// ignore-stage1
1313

14-
#![feature(proc_macro_path_invoc)]
14+
#![feature(proc_macro_path_invoc, unrestricted_attribute_tokens)]
1515

1616
extern crate derive_b;
1717

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 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+
// ignore-tidy-linelength
12+
13+
// Test that `#[rustc_*]` attributes are gated by `rustc_attrs` feature gate.
14+
15+
#[rustc_variance] //~ ERROR the `#[rustc_variance]` attribute is just used for rustc unit tests and will never be stable
16+
#[rustc_error] //~ ERROR the `#[rustc_error]` attribute is just used for rustc unit tests and will never be stable
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0658]: the `#[rustc_variance]` attribute is just used for rustc unit tests and will never be stable (see issue #29642)
2+
--> $DIR/feature-gate-rustc-attrs-1.rs:15:1
3+
|
4+
LL | #[rustc_variance] //~ ERROR the `#[rustc_variance]` attribute is just used for rustc unit tests and will never be stable
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
8+
9+
error[E0658]: the `#[rustc_error]` attribute is just used for rustc unit tests and will never be stable (see issue #29642)
10+
--> $DIR/feature-gate-rustc-attrs-1.rs:16:1
11+
|
12+
LL | #[rustc_error] //~ ERROR the `#[rustc_error]` attribute is just used for rustc unit tests and will never be stable
13+
| ^^^^^^^^^^^^^^
14+
|
15+
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/feature-gate-rustc-attrs.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
// Test that `#[rustc_*]` attributes are gated by `rustc_attrs` feature gate.
1414

15-
#[rustc_variance] //~ ERROR the `#[rustc_variance]` attribute is just used for rustc unit tests and will never be stable
16-
#[rustc_error] //~ ERROR the `#[rustc_error]` attribute is just used for rustc unit tests and will never be stable
1715
#[rustc_foo]
1816
//~^ ERROR unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
1917

0 commit comments

Comments
 (0)