Skip to content
Merged
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
12 changes: 11 additions & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3445,6 +3445,7 @@ impl NormalAttr {
unsafety: Safety::Default,
path: Path::from_ident(ident),
args: AttrArgs::Empty,
span: ident.span,
},
tokens: None,
}
Expand All @@ -3456,6 +3457,15 @@ pub struct AttrItem {
pub unsafety: Safety,
pub path: Path,
pub args: AttrArgs,
/// The span of the entire attr item. For parse attrs this excludes `#[`/`]`. E.g.:
/// ```ignore (illustrative)
/// #[foo(bar)]
/// ^^^^^^^^
/// #[unsafe(no_mangle)]
/// ^^^^^^^^^^^^^^^^^
/// ```
/// For internally constructed spans (`mk_attr_*`) the exact meaning may differ.
pub span: Span,
}

/// Synthetic attributes are inserted by the compiler. They cannot be written in source code, and
Expand Down Expand Up @@ -4398,7 +4408,7 @@ mod size_asserts {
static_assert_size!(MetaItem, 80);
static_assert_size!(MetaItemKind, 40);
static_assert_size!(MetaItemLit, 40);
static_assert_size!(NormalAttr, 72);
static_assert_size!(NormalAttr, 80);
static_assert_size!(Param, 40);
static_assert_size!(Pat, 64);
static_assert_size!(PatKind, 48);
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl Attribute {

// #[deprecated = "..."]
if let Some(s) = meta.value_str() {
return Some(Ident { name: s, span: meta.span() });
return Some(Ident { name: s, span: meta.span });
}

// #[deprecated(note = "...")]
Expand All @@ -342,10 +342,6 @@ impl AttrItem {
if let [seg] = &*self.path.segments { Some(seg.ident.name) } else { None }
}

pub fn span(&self) -> Span {
self.args.span().map_or(self.path.span, |args_span| self.path.span.to(args_span))
}

pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
match &self.args {
AttrArgs::Delimited(args) if args.delim == Delimiter::Parenthesis => {
Expand Down Expand Up @@ -794,6 +790,8 @@ fn mk_attr_tokens(
LazyAttrTokenStream::new_direct(AttrTokenStream::new(tokens))
}

// `span` is used for the `Attribute` and everything within it (except for any span within
// `unsafety`).
pub fn mk_attr_word(
g: &AttrIdGenerator,
style: AttrStyle,
Expand All @@ -814,9 +812,11 @@ pub fn mk_attr_word(
span,
));

mk_attr_from_item(g, AttrItem { unsafety, path, args }, tokens, style, span)
mk_attr_from_item(g, AttrItem { unsafety, path, args, span }, tokens, style, span)
}

// `span` is used for the `Attribute` and everything within it (except for any span within
// `unsafety`).
pub fn mk_attr_nested_word(
g: &AttrIdGenerator,
style: AttrStyle,
Expand Down Expand Up @@ -855,9 +855,11 @@ pub fn mk_attr_nested_word(
span,
));

mk_attr_from_item(g, AttrItem { unsafety, path, args: attr_args }, tokens, style, span)
mk_attr_from_item(g, AttrItem { unsafety, path, args: attr_args, span }, tokens, style, span)
}

// `span` is used for the `Attribute` and everything within it (except for any span within
// `unsafety`).
pub fn mk_attr_name_value_str(
g: &AttrIdGenerator,
style: AttrStyle,
Expand Down Expand Up @@ -891,7 +893,7 @@ pub fn mk_attr_name_value_str(
span,
));

mk_attr_from_item(g, AttrItem { unsafety, path, args }, tokens, style, span)
mk_attr_from_item(g, AttrItem { unsafety, path, args, span }, tokens, style, span)
}

pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr_parsing/src/attributes/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ fn parse_cfg_attr_internal<'a>(
let cfg_predicate = AttributeParser::parse_single_args(
sess,
attribute.span,
attribute.get_normal_item().span(),
attribute.get_normal_item().span,
attribute.style,
AttrPath { segments: attribute.path().into_boxed_slice(), span: attribute.span },
Some(attribute.get_normal_item().unsafety),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_attr_parsing/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl<'sess> AttributeParser<'sess> {
Self::parse_single_args(
sess,
attr.span,
attr_item.span(),
attr_item.span,
attr.style,
path,
Some(attr_item.unsafety),
Expand Down Expand Up @@ -334,7 +334,7 @@ impl<'sess> AttributeParser<'sess> {
let attr_path = AttrPath::from_ast(&n.item.path, lower_span);
let parts =
n.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
let inner_span = lower_span(n.item.span());
let inner_span = lower_span(n.item.span);

if let Some(accept) = ATTRIBUTE_PARSERS.accepters.get(parts.as_slice()) {
self.check_attribute_safety(
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_builtin_macros/src/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod llvm_enzyme {
};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_hir::attrs::RustcAutodiff;
use rustc_span::{Ident, Span, Symbol, kw, sym};
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
use thin_vec::{ThinVec, thin_vec};
use tracing::{debug, trace};

Expand Down Expand Up @@ -362,6 +362,7 @@ mod llvm_enzyme {
unsafety: ast::Safety::Default,
path: ast::Path::from_ident(Ident::with_dummy_span(sym::inline)),
args: ast::AttrArgs::Delimited(never_arg),
span: DUMMY_SP,
};
let inline_never_attr = Box::new(ast::NormalAttr { item: inline_item, tokens: None });
let new_id = ecx.sess.psess.attr_id_generator.mk_attr_id();
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ impl<'a> TraitDef<'a> {
})
.collect(),
}),
span: self.span,
},
self.span,
),
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_builtin_macros/src/offload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
use rustc_ast::{AttrItem, ast};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_session::config::Offload;
use rustc_span::{Ident, Span, sym};
use rustc_span::{DUMMY_SP, Ident, Span, sym};
use thin_vec::thin_vec;

use crate::diagnostics;
Expand Down Expand Up @@ -115,6 +115,7 @@ pub(crate) fn expand_kernel(
unsafety: ast::Safety::Unsafe(span),
path: ast::Path::from_ident(Ident::new(sym::no_mangle, span)),
args: ast::AttrArgs::Empty,
span,
};

let no_mangle_attr = Box::new(ast::NormalAttr { item: unsafe_item, tokens: None });
Expand Down Expand Up @@ -179,6 +180,7 @@ pub(crate) fn expand_kernel(
unsafety: ast::Safety::Default,
path: ast::Path::from_ident(Ident::with_dummy_span(sym::inline)),
args: ast::AttrArgs::Delimited(never_arg),
span: DUMMY_SP,
};
let inline_never_attr = Box::new(ast::NormalAttr { item: inline_item, tokens: None });

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ fn transcribe_pnr<'tx>(
ParseNtResult::Meta(attr_item) => {
let has_meta_form = attr_item.node.meta_kind().is_some();
mk_delimited(
attr_item.node.span(),
attr_item.node.span,
MetaVarKind::Meta { has_meta_form },
TokenStream::from_ast(attr_item),
)
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_parse/src/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ impl<'a> Parser<'a> {

// Attr items don't have attributes.
self.collect_tokens(None, AttrWrapper::empty(), force_collect, |this, _empty_attrs| {
let lo = this.token.span;
let is_unsafe = this.eat_keyword(exp!(Unsafe));
let unsafety = if is_unsafe {
let unsafe_span = this.prev_token.span;
Expand All @@ -349,8 +350,9 @@ impl<'a> Parser<'a> {
if is_unsafe {
this.expect(exp!(CloseParen))?;
}
let span = lo.to(this.prev_token.span);
Ok((
WithTokens::new(ast::AttrItem { unsafety, path, args }),
WithTokens::new(ast::AttrItem { unsafety, path, args, span }),
Trailing::No,
UsePreAttrPos::No,
))
Expand Down
40 changes: 20 additions & 20 deletions tests/ui/lint/lint-unsafe-code.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -190,34 +190,34 @@ LL | unsafe_in_macro!()
= note: this error originates in the macro `unsafe_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

error: usage of the unsafe `#[naked]` attribute
--> $DIR/lint-unsafe-code.rs:139:10
--> $DIR/lint-unsafe-code.rs:139:3
|
LL | #[unsafe(naked)] fn naked1() { naked_asm!("halt") }
| ^^^^^
| ^^^^^^^^^^^^^
|
= note: the `#[naked]` attribute adds the safety obligation that the function's body must respect the function’s calling convention, uphold its signature, and either return or diverge (i.e., not fall through past the end of the assembly code).

error: usage of the unsafe `#[force_target_feature]` attribute
--> $DIR/lint-unsafe-code.rs:168:10
--> $DIR/lint-unsafe-code.rs:168:3
|
LL | #[unsafe(force_target_feature(enable = "avx2"))] fn force_target_feature() { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: a function with the signature of the function the attribute is applied to must only be callable if the force-enabled features are guaranteed to be present

error: usage of the unsafe `#[naked]` attribute
--> $DIR/lint-unsafe-code.rs:149:14
--> $DIR/lint-unsafe-code.rs:149:7
|
LL | #[unsafe(naked)] fn naked3() { naked_asm!("halt") }
| ^^^^^
| ^^^^^^^^^^^^^
|
= note: the `#[naked]` attribute adds the safety obligation that the function's body must respect the function’s calling convention, uphold its signature, and either return or diverge (i.e., not fall through past the end of the assembly code).

error: usage of the unsafe `#[force_target_feature]` attribute
--> $DIR/lint-unsafe-code.rs:178:14
--> $DIR/lint-unsafe-code.rs:178:7
|
LL | #[unsafe(force_target_feature(enable = "avx2"))] fn force_target_feature() { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: a function with the signature of the function the attribute is applied to must only be callable if the force-enabled features are guaranteed to be present

Expand Down Expand Up @@ -254,50 +254,50 @@ LL | #[export_name = "bar"] fn foo() {}
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them

error: usage of the unsafe `#[naked]` attribute
--> $DIR/lint-unsafe-code.rs:144:14
--> $DIR/lint-unsafe-code.rs:144:7
|
LL | #[unsafe(naked)] fn naked2() { naked_asm!("halt") }
| ^^^^^
| ^^^^^^^^^^^^^
|
= note: the `#[naked]` attribute adds the safety obligation that the function's body must respect the function’s calling convention, uphold its signature, and either return or diverge (i.e., not fall through past the end of the assembly code).

error: usage of the unsafe `#[naked]` attribute
--> $DIR/lint-unsafe-code.rs:154:14
--> $DIR/lint-unsafe-code.rs:154:7
|
LL | #[unsafe(naked)] fn naked4() { naked_asm!("halt") }
| ^^^^^
| ^^^^^^^^^^^^^
|
= note: the `#[naked]` attribute adds the safety obligation that the function's body must respect the function’s calling convention, uphold its signature, and either return or diverge (i.e., not fall through past the end of the assembly code).

error: usage of the unsafe `#[force_target_feature]` attribute
--> $DIR/lint-unsafe-code.rs:173:14
--> $DIR/lint-unsafe-code.rs:173:7
|
LL | #[unsafe(force_target_feature(enable = "avx2"))] fn force_target_feature() { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: a function with the signature of the function the attribute is applied to must only be callable if the force-enabled features are guaranteed to be present

error: usage of the unsafe `#[force_target_feature]` attribute
--> $DIR/lint-unsafe-code.rs:182:14
--> $DIR/lint-unsafe-code.rs:182:7
|
LL | #[unsafe(force_target_feature(enable = "avx2"))] fn force_target_feature() { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: a function with the signature of the function the attribute is applied to must only be callable if the force-enabled features are guaranteed to be present

error: usage of the unsafe `#[ffi_pure]` attribute
--> $DIR/lint-unsafe-code.rs:159:14
--> $DIR/lint-unsafe-code.rs:159:7
|
LL | #[unsafe(ffi_pure)]
| ^^^^^^^^
| ^^^^^^^^^^^^^^^^
|
= note: `#[ffi_pure]` functions shall have no effects except for its return value, which shall not change across two consecutive function calls with the same parameters.

error: usage of the unsafe `#[ffi_const]` attribute
--> $DIR/lint-unsafe-code.rs:163:14
--> $DIR/lint-unsafe-code.rs:163:7
|
LL | #[unsafe(ffi_const)]
| ^^^^^^^^^
| ^^^^^^^^^^^^^^^^^
|
= note: `#[ffi_const]` functions shall have no effects except for its return value, which can only depend on the values of the function parameters, and is not affected by changes to the observable state of the program.

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/proc-macro/capture-macro-rules-invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ macro_rules! use_pat {
}
}

macro_rules! use_meta {
($meta:meta) => {
print_bang_consume!($meta)
}
}

#[allow(dead_code)]
struct Foo;
impl Foo {
Expand All @@ -47,6 +53,7 @@ impl Foo {
[ a b c ],
-30
);
use_meta!(unsafe(no_mangle));
}

fn with_pat(use_pat!((a, b)): (u32, u32)) {
Expand Down
Loading
Loading