Skip to content

Fix incorrect cfg structured suggestion and make suggestion verbose #137773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3034,6 +3034,7 @@ impl NormalAttr {
path: Path::from_ident(ident),
args: AttrArgs::Empty,
tokens: None,
span: ident.span,
},
tokens: None,
}
Expand All @@ -3047,6 +3048,7 @@ pub struct AttrItem {
pub args: AttrArgs,
// Tokens for the meta item, e.g. just the `foo` within `#[foo]` or `#![foo]`.
pub tokens: Option<LazyAttrTokenStream>,
pub span: Span,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls add docs with examples what it refers to

}

impl AttrItem {
Expand Down
26 changes: 18 additions & 8 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Attribute {
/// Extracts the MetaItem from inside this Attribute.
pub fn meta(&self) -> Option<MetaItem> {
match &self.kind {
AttrKind::Normal(normal) => normal.item.meta(self.span),
AttrKind::Normal(normal) => normal.item.meta(),
AttrKind::DocComment(..) => None,
}
}
Expand Down Expand Up @@ -285,12 +285,12 @@ impl AttrItem {
}
}

pub fn meta(&self, span: Span) -> Option<MetaItem> {
pub fn meta(&self) -> Option<MetaItem> {
Some(MetaItem {
unsafety: Safety::Default,
path: self.path.clone(),
kind: self.meta_kind()?,
span,
span: self.span(),
})
}

Expand Down Expand Up @@ -406,7 +406,7 @@ impl MetaItem {
Path { span, segments, tokens: None }
}
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
token::Nonterminal::NtMeta(item) => return item.meta(),
token::Nonterminal::NtPath(path) => (**path).clone(),
_ => return None,
},
Expand Down Expand Up @@ -620,8 +620,15 @@ pub fn mk_attr(
path: Path,
args: AttrArgs,
span: Span,
inner_span: Span,
) -> Attribute {
mk_attr_from_item(g, AttrItem { unsafety, path, args, tokens: None }, None, style, span)
mk_attr_from_item(
g,
AttrItem { unsafety, path, args, tokens: None, span: inner_span },
None,
style,
span,
)
}

pub fn mk_attr_from_item(
Expand All @@ -645,10 +652,11 @@ pub fn mk_attr_word(
unsafety: Safety,
name: Symbol,
span: Span,
inner_span: Span,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no caller needs this to be different as there are no args

) -> Attribute {
let path = Path::from_ident(Ident::new(name, span));
let args = AttrArgs::Empty;
mk_attr(g, style, unsafety, path, args, span)
mk_attr(g, style, unsafety, path, args, span, inner_span)
}

pub fn mk_attr_nested_word(
Expand All @@ -658,6 +666,7 @@ pub fn mk_attr_nested_word(
outer: Symbol,
inner: Symbol,
span: Span,
inner_span: Span,
) -> Attribute {
let inner_tokens = TokenStream::new(vec![TokenTree::Token(
Token::from_ast_ident(Ident::new(inner, span)),
Expand All @@ -670,7 +679,7 @@ pub fn mk_attr_nested_word(
delim: Delimiter::Parenthesis,
tokens: inner_tokens,
});
mk_attr(g, style, unsafety, path, attr_args, span)
mk_attr(g, style, unsafety, path, attr_args, span, inner_span)
}

pub fn mk_attr_name_value_str(
Expand All @@ -680,6 +689,7 @@ pub fn mk_attr_name_value_str(
name: Symbol,
val: Symbol,
span: Span,
inner_span: Span,
) -> Attribute {
let lit = token::Lit::new(token::Str, escape_string_symbol(val), None);
let expr = P(Expr {
Expand All @@ -691,7 +701,7 @@ pub fn mk_attr_name_value_str(
});
let path = Path::from_ident(Ident::new(name, span));
let args = AttrArgs::Eq { eq_span: span, expr };
mk_attr(g, style, unsafety, path, args, span)
mk_attr(g, style, unsafety, path, args, span, inner_span)
}

pub fn filter_by_name<A: AttributeExt>(attrs: &[A], name: Symbol) -> impl Iterator<Item = &A> {
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,13 +723,14 @@ fn walk_attribute<T: MutVisitor>(vis: &mut T, attr: &mut Attribute) {
match kind {
AttrKind::Normal(normal) => {
let NormalAttr {
item: AttrItem { unsafety: _, path, args, tokens },
item: AttrItem { unsafety: _, path, args, tokens, span },
tokens: attr_tokens,
} = &mut **normal;
vis.visit_path(path);
visit_attr_args(vis, args);
visit_lazy_tts(vis, tokens);
visit_lazy_tts(vis, attr_tokens);
vis.visit_span(span);
}
AttrKind::DocComment(_kind, _sym) => {}
}
Expand Down Expand Up @@ -909,10 +910,11 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
token::NtExpr(expr) => vis.visit_expr(expr),
token::NtLiteral(expr) => vis.visit_expr(expr),
token::NtMeta(item) => {
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
let AttrItem { unsafety: _, path, args, tokens, span } = item.deref_mut();
vis.visit_path(path);
visit_attr_args(vis, args);
visit_lazy_tts(vis, tokens);
vis.visit_span(span);
}
token::NtPath(path) => vis.visit_path(path),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute)
match kind {
AttrKind::Normal(normal) => {
let NormalAttr { item, tokens: _ } = &**normal;
let AttrItem { unsafety: _, path, args, tokens: _ } = item;
let AttrItem { unsafety: _, path, args, tokens: _, span: _ } = item;
try_visit!(visitor.visit_path(path, DUMMY_NODE_ID));
try_visit!(walk_attr_args(visitor, args));
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
sym::allow,
sym::unreachable_code,
try_span,
try_span,
);
let attrs: AttrVec = thin_vec![attr];

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ pub fn print_crate<'a>(
sym::feature,
sym::prelude_import,
DUMMY_SP,
DUMMY_SP,
);
s.print_attribute(&fake_attr);

Expand All @@ -270,6 +271,7 @@ pub fn print_crate<'a>(
Safety::Default,
sym::no_std,
DUMMY_SP,
DUMMY_SP,
);
s.print_attribute(&fake_attr);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/alloc_error_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
body,
}));

let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)];
let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span, span)];

let item = cx.item(span, Ident::from_str_and_span("__rg_oom", span), attrs, kind);
cx.stmt_item(sig_span, item)
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_builtin_macros/src/assert/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ impl<'cx, 'a> Context<'cx, 'a> {
self.cx.item(
self.span,
Ident::empty(),
thin_vec![self.cx.attr_nested_word(sym::allow, sym::unused_imports, self.span)],
thin_vec![self.cx.attr_nested_word(
sym::allow,
sym::unused_imports,
self.span,
self.span
)],
ItemKind::Use(UseTree {
prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])),
kind: UseTreeKind::Nested {
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_builtin_macros/src/cmdline_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
raw_attr.clone(),
));

let start_span = parser.token.span;
let AttrItem { unsafety, path, args, tokens: _ } =
let AttrItem { unsafety, path, args, tokens: _, span } =
match parser.parse_attr_item(ForceCollect::No) {
Ok(ai) => ai,
Err(err) => {
err.emit();
continue;
}
};
let end_span = parser.token.span;
if parser.token != token::Eof {
psess.dcx().emit_err(errors::InvalidCrateAttr { span: start_span.to(end_span) });
psess.dcx().emit_err(errors::InvalidCrateAttr { span });
continue;
}

Expand All @@ -38,7 +36,8 @@ pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
unsafety,
path,
args,
start_span.to(end_span),
span,
span,
));
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub(crate) fn expand_deriving_clone(
explicit_self: true,
nonself_args: Vec::new(),
ret_ty: Self_,
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
fieldless_variants_strategy: FieldlessVariantsStrategy::Default,
combine_substructure: substructure,
}],
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub(crate) fn expand_deriving_eq(
nonself_args: vec![],
ret_ty: Unit,
attributes: thin_vec![
cx.attr_word(sym::inline, span),
cx.attr_nested_word(sym::doc, sym::hidden, span),
cx.attr_nested_word(sym::coverage, sym::off, span)
cx.attr_word(sym::inline, span, span),
cx.attr_nested_word(sym::doc, sym::hidden, span, span),
cx.attr_nested_word(sym::coverage, sym::off, span, span)
],
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
combine_substructure: combine_substructure(Box::new(|a, b, c| {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) fn expand_deriving_ord(
explicit_self: true,
nonself_args: vec![(self_ref(), sym::other)],
ret_ty: Path(path_std!(cmp::Ordering)),
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
combine_substructure: combine_substructure(Box::new(|a, b, c| cs_cmp(a, b, c))),
}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub(crate) fn expand_deriving_partial_eq(
explicit_self: true,
nonself_args: vec![(self_ref(), sym::other)],
ret_ty: Path(path_local!(bool)),
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
combine_substructure: combine_substructure(Box::new(|a, b, c| cs_eq(a, b, c))),
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(crate) fn expand_deriving_partial_ord(
explicit_self: true,
nonself_args: vec![(self_ref(), sym::other)],
ret_ty,
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
cs_partial_cmp(cx, span, substr, discr_then_data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub(crate) fn expand_deriving_coerce_pointee(

// Declare helper function that adds implementation blocks.
// FIXME(dingxiangfei2009): Investigate the set of attributes on target struct to be propagated to impls
let attrs = thin_vec![cx.attr_word(sym::automatically_derived, span),];
let attrs = thin_vec![cx.attr_word(sym::automatically_derived, span, span),];
// # Validity assertion which will be checked later in `rustc_hir_analysis::coherence::builtins`.
{
let trait_path =
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) fn expand_deriving_debug(
explicit_self: true,
nonself_args: vec![(fmtr, sym::f)],
ret_ty: Path(path_std!(fmt::Result)),
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
fieldless_variants_strategy:
FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless,
combine_substructure: combine_substructure(Box::new(|a, b, c| {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn expand_deriving_default(
explicit_self: false,
nonself_args: Vec::new(),
ret_ty: Self_,
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
fieldless_variants_strategy: FieldlessVariantsStrategy::Default,
combine_substructure: combine_substructure(Box::new(|cx, trait_span, substr| {
match substr.fields {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ impl<'a> TraitDef<'a> {
let path = cx.path_all(self.span, false, vec![type_ident], self_params);
let self_type = cx.ty_path(path);

let attrs = thin_vec![cx.attr_word(sym::automatically_derived, self.span),];
let attrs = thin_vec![cx.attr_word(sym::automatically_derived, self.span, self.span)];
let opt_trait_ref = Some(trait_ref);

cx.item(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) fn expand_deriving_hash(
explicit_self: true,
nonself_args: vec![(Ref(Box::new(Path(arg)), Mutability::Mut), sym::state)],
ret_ty: Unit,
attributes: thin_vec![cx.attr_word(sym::inline, span)],
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
combine_substructure: combine_substructure(Box::new(|a, b, c| {
hash_substructure(a, b, c)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl AllocFnFactory<'_, '_> {
}

fn attrs(&self) -> AttrVec {
thin_vec![self.cx.attr_word(sym::rustc_std_internal_symbol, self.span)]
thin_vec![self.cx.attr_word(sym::rustc_std_internal_symbol, self.span, self.span)]
}

fn arg_ty(&self, input: &AllocatorMethodInput, args: &mut ThinVec<Param>) -> P<Expr> {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_builtin_macros/src/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
cx.expr_array_ref(span, decls),
)
.map(|mut i| {
i.attrs.push(cx.attr_word(sym::rustc_proc_macro_decls, span));
i.attrs.push(cx.attr_word(sym::used, span));
i.attrs.push(cx.attr_nested_word(sym::allow, sym::deprecated, span));
i.attrs.push(cx.attr_word(sym::rustc_proc_macro_decls, span, span));
i.attrs.push(cx.attr_word(sym::used, span, span));
i.attrs.push(cx.attr_nested_word(sym::allow, sym::deprecated, span, span));
i
});

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/standard_library_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn inject(
let item = cx.item(
span,
Ident::new(name, ident_span),
thin_vec![cx.attr_word(sym::macro_use, span)],
thin_vec![cx.attr_word(sym::macro_use, span, span)],
ast::ItemKind::ExternCrate(None),
);

Expand All @@ -68,7 +68,7 @@ pub fn inject(
let use_item = cx.item(
span,
Ident::empty(),
thin_vec![cx.attr_word(sym::prelude_import, span)],
thin_vec![cx.attr_word(sym::prelude_import, span, span)],
ast::ItemKind::Use(ast::UseTree {
prefix: cx.path(span, import_path),
kind: ast::UseTreeKind::Glob,
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn expand_test_case(
tokens: None,
};
item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
item.attrs.push(ecx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, sp));
item.attrs.push(ecx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, sp, sp));
item
});

Expand Down Expand Up @@ -200,7 +200,7 @@ pub(crate) fn expand_test_or_bench(
// corresponding macro declaration in `core::macros`.
let coverage_off = |mut expr: P<ast::Expr>| {
assert_matches!(expr.kind, ast::ExprKind::Closure(_));
expr.attrs.push(cx.attr_nested_word(sym::coverage, sym::off, sp));
expr.attrs.push(cx.attr_nested_word(sym::coverage, sym::off, sp, sp));
expr
};

Expand Down Expand Up @@ -273,11 +273,11 @@ pub(crate) fn expand_test_or_bench(
Ident::new(item.ident.name, sp),
thin_vec![
// #[cfg(test)]
cx.attr_nested_word(sym::cfg, sym::test, attr_sp),
cx.attr_nested_word(sym::cfg, sym::test, attr_sp, attr_sp),
// #[rustc_test_marker = "test_case_sort_key"]
cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp),
cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp, attr_sp),
// #[doc(hidden)]
cx.attr_nested_word(sym::doc, sym::hidden, attr_sp),
cx.attr_nested_word(sym::doc, sym::hidden, attr_sp, attr_sp),
],
// const $ident: test::TestDescAndFn =
ast::ItemKind::Const(
Expand Down
Loading
Loading