提交 bf1e70cd 编写于 作者: V Vadim Petrochenkov

resolve: Prohibit use of imported non-macro attributes

上级 e1d1487f
......@@ -240,7 +240,7 @@ pub fn from_hir(vdata: &hir::VariantData) -> 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",
......
......@@ -376,6 +376,7 @@ pub fn resolve_macro_to_def_inner(
.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 @@ pub fn resolve_macro_to_def_inner(
.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 @@ pub fn finalize_current_module_macro_resolutions(&mut self) {
}
}
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.
......
// 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() {}
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
......@@ -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 @@
type A = imported_u8;
#[imported_inline]
#[imported_rustfmt::skip]
fn main() {
imported_vec![0];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册