Skip to content

Commit

Permalink
resolve: Prohibit use of imported non-macro attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jan 12, 2019
1 parent e1d1487 commit bf1e70c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/def.rs
Expand Up @@ -240,7 +240,7 @@ impl CtorKind {
}

impl NonMacroAttrKind {
fn descr(self) -> &'static str {
pub fn descr(self) -> &'static str {
match self {
NonMacroAttrKind::Builtin => "built-in attribute",
NonMacroAttrKind::Tool => "tool attribute",
Expand Down
19 changes: 18 additions & 1 deletion src/librustc_resolve/macros.rs
Expand Up @@ -376,6 +376,7 @@ impl<'a> Resolver<'a> {
.push((path, path_span, kind, parent_scope.clone(), def.ok()));
}

self.prohibit_imported_non_macro_attrs(None, def.ok(), path_span);
def
} else {
let binding = self.early_resolve_ident_in_lexical_scope(
Expand All @@ -390,7 +391,9 @@ impl<'a> Resolver<'a> {
.push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
}

binding.map(|binding| binding.def())
let def = binding.map(|binding| binding.def());
self.prohibit_imported_non_macro_attrs(binding.ok(), def.ok(), path_span);
def
}
}

Expand Down Expand Up @@ -982,6 +985,20 @@ impl<'a> Resolver<'a> {
}
}

fn prohibit_imported_non_macro_attrs(&self, binding: Option<&'a NameBinding<'a>>,
def: Option<Def>, span: Span) {
if let Some(Def::NonMacroAttr(kind)) = def {
if kind != NonMacroAttrKind::Tool && binding.map_or(true, |b| b.is_import()) {
let msg = format!("cannot use a {} through an import", kind.descr());
let mut err = self.session.struct_span_err(span, &msg);
if let Some(binding) = binding {
err.span_note(binding.span, &format!("the {} imported here", kind.descr()));
}
err.emit();
}
}
}

fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
err: &mut DiagnosticBuilder<'a>, span: Span) {
// First check if this is a locally-defined bang macro.
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/rust-2018/uniform-paths/prelude-fail-2.rs
@@ -0,0 +1,9 @@
// edition:2018

#![feature(uniform_paths)]

// Built-in attribute
use inline as imported_inline;

#[imported_inline] //~ ERROR cannot use a built-in attribute through an import
fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/rust-2018/uniform-paths/prelude-fail-2.stderr
@@ -0,0 +1,14 @@
error: cannot use a built-in attribute through an import
--> $DIR/prelude-fail-2.rs:8:3
|
LL | #[imported_inline] //~ ERROR cannot use a built-in attribute through an import
| ^^^^^^^^^^^^^^^
|
note: the built-in attribute imported here
--> $DIR/prelude-fail-2.rs:6:5
|
LL | use inline as imported_inline;
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

4 changes: 0 additions & 4 deletions src/test/ui/rust-2018/uniform-paths/prelude.rs
Expand Up @@ -6,9 +6,6 @@
// Macro imported with `#[macro_use] extern crate`
use vec as imported_vec;

// Built-in attribute
use inline as imported_inline;

// Tool module
use rustfmt as imported_rustfmt;

Expand All @@ -20,7 +17,6 @@ use u8 as imported_u8;

type A = imported_u8;

#[imported_inline]
#[imported_rustfmt::skip]
fn main() {
imported_vec![0];
Expand Down

0 comments on commit bf1e70c

Please sign in to comment.