Skip to content

Commit bf1e70c

Browse files
committed
resolve: Prohibit use of imported non-macro attributes
1 parent e1d1487 commit bf1e70c

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
@@ -240,7 +240,7 @@ impl CtorKind {
240240
}
241241

242242
impl NonMacroAttrKind {
243-
fn descr(self) -> &'static str {
243+
pub fn descr(self) -> &'static str {
244244
match self {
245245
NonMacroAttrKind::Builtin => "built-in attribute",
246246
NonMacroAttrKind::Tool => "tool attribute",

src/librustc_resolve/macros.rs

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

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

393-
binding.map(|binding| binding.def())
394+
let def = binding.map(|binding| binding.def());
395+
self.prohibit_imported_non_macro_attrs(binding.ok(), def.ok(), path_span);
396+
def
394397
}
395398
}
396399

@@ -982,6 +985,20 @@ impl<'a> Resolver<'a> {
982985
}
983986
}
984987

988+
fn prohibit_imported_non_macro_attrs(&self, binding: Option<&'a NameBinding<'a>>,
989+
def: Option<Def>, span: Span) {
990+
if let Some(Def::NonMacroAttr(kind)) = def {
991+
if kind != NonMacroAttrKind::Tool && binding.map_or(true, |b| b.is_import()) {
992+
let msg = format!("cannot use a {} through an import", kind.descr());
993+
let mut err = self.session.struct_span_err(span, &msg);
994+
if let Some(binding) = binding {
995+
err.span_note(binding.span, &format!("the {} imported here", kind.descr()));
996+
}
997+
err.emit();
998+
}
999+
}
1000+
}
1001+
9851002
fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
9861003
err: &mut DiagnosticBuilder<'a>, span: Span) {
9871004
// 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)