Skip to content

Commit

Permalink
Add more hacks necessary for bootstrapping rustc
Browse files Browse the repository at this point in the history
Fix visiting order for attributes on modules, it's significant for `macro_rules!` resolution
  • Loading branch information
petrochenkov committed Sep 3, 2018
1 parent 90641fc commit bc49117
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 57 deletions.
17 changes: 12 additions & 5 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,17 +943,22 @@ impl<'a, 'cl> Resolver<'a, 'cl> {

for &(ident, parent_expansion, parent_legacy_scope)
in module.builtin_attrs.borrow().iter() {
let resolution = if ident.name == "doc" {
let resolution = if ident.name == "doc" || ident.name == "test" {
// HACK: Some sugared doc comments in macros lose their memory about being
// sugared doc comments and become shadowed by other macros.
// We need to come up with some more principled approach instead.
None
} else {
self.resolve_legacy_scope(ident, parent_expansion, parent_legacy_scope, true)
}.or_else(|| {
self.resolve_lexical_macro_path_segment(ident, MacroNS, parent_expansion, true,
true, true, ident.span)
.map(|(binding, _)| binding).ok()
let binding = self.resolve_lexical_macro_path_segment(
ident, MacroNS, parent_expansion, true, true, true, ident.span
).map(|(binding, _)| binding).ok();
match binding.map(|binding| binding.def_ignoring_ambiguity()) {
// HACK: More hacks necessary for bootstrapping rustc.
Some(Def::Macro(_, MacroKind::Bang)) if ident.name == "warn" => None,
_ => binding,
}
});

if let Some(binding) = resolution {
Expand Down Expand Up @@ -1082,7 +1087,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
self.define(module, ident, MacroNS,
(def, vis, item.span, expansion, IsMacroExport));
} else {
self.check_reserved_macro_name(ident, MacroNS);
if !attr::contains_name(&item.attrs, "rustc_doc_only_macro") {
self.check_reserved_macro_name(ident, MacroNS);
}
self.unused_macros.insert(def_id);
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
}

pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
walk_list!(visitor, visit_attribute, &krate.attrs);
visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
}

pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
Expand Down Expand Up @@ -217,6 +217,7 @@ pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitR
}

pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
walk_list!(visitor, visit_attribute, &item.attrs);
visitor.visit_vis(&item.vis);
visitor.visit_ident(item.ident);
match item.node {
Expand Down Expand Up @@ -288,7 +289,6 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
}
walk_list!(visitor, visit_attribute, &item.attrs);
}

pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V,
Expand Down
34 changes: 17 additions & 17 deletions src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -187,56 +187,56 @@ LL | mod inner { #![macro_escape] }
= help: consider an outer attribute, #[macro_use] mod ...

warning: `repr` attribute isn't configurable with a literal
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:317:17
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:63:1
|
LL | mod inner { #![repr="3900"] }
| ^^^^^^^^^^^^^^^ needs a hint
LL | #![repr = "3900"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ needs a hint
|
= note: #[warn(bad_repr)] on by default
= help: valid hints include `#[repr(C)]`, `#[repr(packed)]`, `#[repr(rust)]` and `#[repr(transparent)]`
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html>

warning: `repr` attribute isn't configurable with a literal
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:321:5
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:313:1
|
LL | #[repr = "3900"] fn f() { }
| ^^^^^^^^^^^^^^^^ needs a hint
LL | #[repr = "3900"]
| ^^^^^^^^^^^^^^^^ needs a hint
|
= help: valid hints include `#[repr(C)]`, `#[repr(packed)]`, `#[repr(rust)]` and `#[repr(transparent)]`
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html>

warning: `repr` attribute isn't configurable with a literal
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:327:5
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:317:17
|
LL | #[repr = "3900"] type T = S;
| ^^^^^^^^^^^^^^^^ needs a hint
LL | mod inner { #![repr="3900"] }
| ^^^^^^^^^^^^^^^ needs a hint
|
= help: valid hints include `#[repr(C)]`, `#[repr(packed)]`, `#[repr(rust)]` and `#[repr(transparent)]`
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html>

warning: `repr` attribute isn't configurable with a literal
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:331:5
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:321:5
|
LL | #[repr = "3900"] impl S { }
LL | #[repr = "3900"] fn f() { }
| ^^^^^^^^^^^^^^^^ needs a hint
|
= help: valid hints include `#[repr(C)]`, `#[repr(packed)]`, `#[repr(rust)]` and `#[repr(transparent)]`
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html>

warning: `repr` attribute isn't configurable with a literal
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:313:1
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:327:5
|
LL | #[repr = "3900"]
| ^^^^^^^^^^^^^^^^ needs a hint
LL | #[repr = "3900"] type T = S;
| ^^^^^^^^^^^^^^^^ needs a hint
|
= help: valid hints include `#[repr(C)]`, `#[repr(packed)]`, `#[repr(rust)]` and `#[repr(transparent)]`
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html>

warning: `repr` attribute isn't configurable with a literal
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:63:1
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:331:5
|
LL | #![repr = "3900"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ needs a hint
LL | #[repr = "3900"] impl S { }
| ^^^^^^^^^^^^^^^^ needs a hint
|
= help: valid hints include `#[repr(C)]`, `#[repr(packed)]`, `#[repr(rust)]` and `#[repr(transparent)]`
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html>
Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/feature-gates/feature-gate-link_args.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
error[E0658]: the `link_args` attribute is experimental and not portable across platforms, it is recommended to use `#[link(name = "foo")] instead (see issue #29596)
--> $DIR/feature-gate-link_args.rs:19:1
|
LL | #![link_args = "-l unexpected_use_as_inner_attr_on_mod"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(link_args)] to the crate attributes to enable

error[E0658]: the `link_args` attribute is experimental and not portable across platforms, it is recommended to use `#[link(name = "foo")] instead (see issue #29596)
--> $DIR/feature-gate-link_args.rs:22:1
|
Expand All @@ -14,14 +22,6 @@ LL | #[link_args = "-l unexected_use_on_non_extern_item"]
|
= help: add #![feature(link_args)] to the crate attributes to enable

error[E0658]: the `link_args` attribute is experimental and not portable across platforms, it is recommended to use `#[link(name = "foo")] instead (see issue #29596)
--> $DIR/feature-gate-link_args.rs:19:1
|
LL | #![link_args = "-l unexpected_use_as_inner_attr_on_mod"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(link_args)] to the crate attributes to enable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
16 changes: 8 additions & 8 deletions src/test/ui/lint/suggestions.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
warning: use of deprecated attribute `no_debug`: the `#[no_debug]` attribute was an experimental feature that has been deprecated due to lack of demand. See https://github.com/rust-lang/rust/issues/29721
--> $DIR/suggestions.rs:57:1
|
LL | #[no_debug] // should suggest removal of deprecated attribute
| ^^^^^^^^^^^ help: remove this attribute
|
= note: #[warn(deprecated)] on by default

warning: unnecessary parentheses around assigned value
--> $DIR/suggestions.rs:64:21
|
Expand All @@ -10,14 +18,6 @@ note: lint level defined here
LL | #![warn(unused_mut, unused_parens)] // UI tests pass `-A unused`—see Issue #43896
| ^^^^^^^^^^^^^

warning: use of deprecated attribute `no_debug`: the `#[no_debug]` attribute was an experimental feature that has been deprecated due to lack of demand. See https://github.com/rust-lang/rust/issues/29721
--> $DIR/suggestions.rs:57:1
|
LL | #[no_debug] // should suggest removal of deprecated attribute
| ^^^^^^^^^^^ help: remove this attribute
|
= note: #[warn(deprecated)] on by default

warning: variable does not need to be mutable
--> $DIR/suggestions.rs:64:13
|
Expand Down
34 changes: 17 additions & 17 deletions src/test/ui/macros/ambiguous-builtin-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ error: name `derive` is reserved in macro namespace
LL | macro derive() {} //~ ERROR name `derive` is reserved in macro namespace
| ^^^^^^

error[E0659]: `feature` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:1:4
|
LL | #![feature(decl_macro)] //~ ERROR `feature` is ambiguous
| ^^^^^^^
|
note: `feature` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:3:1
|
LL | macro feature() {}
| ^^^^^^^^^^^^^^^^^^
note: `feature` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:1:4
|
LL | #![feature(decl_macro)] //~ ERROR `feature` is ambiguous
| ^^^^^^^

error[E0659]: `repr` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:7:3
|
Expand Down Expand Up @@ -135,23 +152,6 @@ note: `repr` could also refer to the name defined here
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
| ^^^^

error[E0659]: `feature` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:1:4
|
LL | #![feature(decl_macro)] //~ ERROR `feature` is ambiguous
| ^^^^^^^
|
note: `feature` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:3:1
|
LL | macro feature() {}
| ^^^^^^^^^^^^^^^^^^
note: `feature` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:1:4
|
LL | #![feature(decl_macro)] //~ ERROR `feature` is ambiguous
| ^^^^^^^

error: aborting due to 11 previous errors

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

0 comments on commit bc49117

Please sign in to comment.