Skip to content

Commit bfb9b95

Browse files
committed
resolve: Prohibit use of imported non-macro attributes
1 parent 98567b5 commit bfb9b95

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

src/librustc/hir/def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl CtorKind {
248248
}
249249

250250
impl NonMacroAttrKind {
251-
fn descr(self) -> &'static str {
251+
pub fn descr(self) -> &'static str {
252252
match self {
253253
NonMacroAttrKind::Builtin => "built-in attribute",
254254
NonMacroAttrKind::Tool => "tool attribute",

src/librustc_resolve/macros.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
499499
.push((path, path_span, kind, parent_scope.clone(), def.ok()));
500500
}
501501

502+
self.prohibit_imported_non_macro_attrs(None, def.ok(), path_span);
502503
def
503504
} else {
504505
let binding = self.early_resolve_ident_in_lexical_scope(
@@ -515,7 +516,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
515516
.push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
516517
}
517518

518-
binding.map(|binding| binding.def_ignoring_ambiguity())
519+
let def = binding.map(|binding| binding.def_ignoring_ambiguity());
520+
self.prohibit_imported_non_macro_attrs(binding.ok(), def.ok(), path_span);
521+
def
519522
}
520523
}
521524

@@ -1098,6 +1101,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10981101
}
10991102
}
11001103

1104+
fn prohibit_imported_non_macro_attrs(&self, binding: Option<&'a NameBinding<'a>>,
1105+
def: Option<Def>, span: Span) {
1106+
if let Some(Def::NonMacroAttr(kind)) = def {
1107+
if kind != NonMacroAttrKind::Tool && binding.map_or(true, |b| b.is_import()) {
1108+
let msg = format!("cannot use a {} through an import", kind.descr());
1109+
let mut err = self.session.struct_span_err(span, &msg);
1110+
if let Some(binding) = binding {
1111+
err.span_note(binding.span, &format!("the {} imported here", kind.descr()));
1112+
}
1113+
err.emit();
1114+
}
1115+
}
1116+
}
1117+
11011118
fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
11021119
err: &mut DiagnosticBuilder<'a>, span: Span) {
11031120
// First check if this is a locally-defined bang macro.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2018
2+
3+
#![feature(uniform_paths)]
4+
5+
// Built-in attribute
6+
use inline as imported_inline;
7+
8+
#[imported_inline] //~ ERROR cannot use a built-in attribute through an import
9+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot use a built-in attribute through an import
2+
--> $DIR/prelude-fail-2.rs:8:3
3+
|
4+
LL | #[imported_inline] //~ ERROR cannot use a built-in attribute through an import
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
note: the built-in attribute imported here
8+
--> $DIR/prelude-fail-2.rs:6:5
9+
|
10+
LL | use inline as imported_inline;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

src/test/ui/rust-2018/uniform-paths/prelude.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
// Macro imported with `#[macro_use] extern crate`
77
use vec as imported_vec;
88

9-
// Built-in attribute
10-
use inline as imported_inline;
11-
129
// Tool module
1310
use rustfmt as imported_rustfmt;
1411

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

2118
type A = imported_u8;
2219

23-
#[imported_inline]
2420
#[imported_rustfmt::skip]
2521
fn main() {
2622
imported_vec![0];

0 commit comments

Comments
 (0)