resolve: Prohibit use of imported non-macro attributes

This commit is contained in:
Vadim Petrochenkov 2018-12-12 04:11:46 +03:00
parent e1d1487fc4
commit bf1e70cd1f
5 changed files with 42 additions and 6 deletions

View file

@ -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",

View file

@ -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(
@ -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
}
}
@ -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.

View file

@ -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() {}

View file

@ -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

View file

@ -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;
@ -20,7 +17,6 @@ use u8 as imported_u8;
type A = imported_u8;
#[imported_inline]
#[imported_rustfmt::skip]
fn main() {
imported_vec![0];