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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3586,6 +3586,7 @@ dependencies = [
"rustc_feature",
"rustc_hir",
"rustc_lexer",
"rustc_lint_defs",
"rustc_macros",
"rustc_parse",
"rustc_parse_format",
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
let (binder_clause, generic_params) = self.lower_closure_binder(binder);

let (body_id, closure_kind) = self.with_new_scopes(fn_decl_span, move |this| {
let mut coroutine_kind = find_attr!(attrs, Coroutine(_) => hir::CoroutineKind::Coroutine(Movability::Movable));
let mut coroutine_kind =
find_attr!(attrs, Coroutine => hir::CoroutineKind::Coroutine(Movability::Movable));

// FIXME(contracts): Support contracts on closures?
let body_id = this.lower_fn_body(decl, None, |this| {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ struct LoweringContext<'a, 'hir> {

impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn new(tcx: TyCtxt<'hir>, resolver: &'a ResolverAstLowering<'hir>) -> Self {
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
Self {
tcx,
resolver,
Expand Down Expand Up @@ -220,7 +219,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
attribute_parser: AttributeParser::new(
tcx.sess,
tcx.features(),
registered_tools,
tcx.registered_tools(()),
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
),
delayed_lints: Vec::new(),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rustc_errors = { path = "../rustc_errors" }
rustc_feature = { path = "../rustc_feature" }
rustc_hir = { path = "../rustc_hir" }
rustc_lexer = { path = "../rustc_lexer" }
rustc_lint_defs = { path = "../rustc_lint_defs" }
rustc_macros = { path = "../rustc_macros" }
rustc_parse = { path = "../rustc_parse" }
rustc_parse_format = { path = "../rustc_parse_format" }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr_parsing/src/attributes/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub(crate) struct CoroutineParser;
impl NoArgsAttributeParser for CoroutineParser {
const PATH: &[Symbol] = &[sym::coroutine];
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Closure)]);
const CREATE: fn(rustc_span::Span) -> AttributeKind = |span| AttributeKind::Coroutine(span);
const CREATE: fn(rustc_span::Span) -> AttributeKind = |_| AttributeKind::Coroutine;
}
24 changes: 16 additions & 8 deletions compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl NoArgsAttributeParser for ColdParser {
Allow(Target::ForeignFn),
Allow(Target::Closure),
]);
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Cold;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::Cold;
}

pub(crate) struct CoverageParser;
Expand Down Expand Up @@ -94,7 +94,7 @@ impl SingleAttributeParser for CoverageParser {
}
};

Some(AttributeKind::Coverage(cx.attr_span, kind))
Some(AttributeKind::Coverage(kind))
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ impl SingleAttributeParser for RustcObjcClassParser {
cx.emit_err(NullOnObjcClass { span: nv.value_span });
return None;
}
Some(AttributeKind::RustcObjcClass { classname, span: cx.attr_span })
Some(AttributeKind::RustcObjcClass { classname })
}
}

Expand All @@ -183,7 +183,7 @@ impl SingleAttributeParser for RustcObjcSelectorParser {
cx.emit_err(NullOnObjcSelector { span: nv.value_span });
return None;
}
Some(AttributeKind::RustcObjcSelector { methname, span: cx.attr_span })
Some(AttributeKind::RustcObjcSelector { methname })
}
}

Expand Down Expand Up @@ -264,10 +264,18 @@ impl AttributeParser for NakedParser {

let span = self.span?;

let Some(tools) = cx.tools else {
unreachable!("tools required while parsing attributes");
};

// only if we found a naked attribute do we do the somewhat expensive check
'outer: for other_attr in cx.all_attrs {
for allowed_attr in ALLOW_LIST {
if other_attr.segments().next().is_some_and(|i| cx.tools.contains(&i.name)) {
if other_attr
.segments()
.next()
.is_some_and(|i| tools.iter().any(|tool| tool.name == i.name))
{
// effectively skips the error message being emitted below
// if it's a tool attribute
continue 'outer;
Expand Down Expand Up @@ -436,9 +444,9 @@ impl AttributeParser for UsedParser {
// If a specific form of `used` is specified, it takes precedence over generic `#[used]`.
// If both `linker` and `compiler` are specified, use `linker`.
Some(match (self.first_compiler, self.first_linker, self.first_default) {
(_, Some(span), _) => AttributeKind::Used { used_by: UsedBy::Linker, span },
(Some(span), _, _) => AttributeKind::Used { used_by: UsedBy::Compiler, span },
(_, _, Some(span)) => AttributeKind::Used { used_by: UsedBy::Default, span },
(_, Some(_), _) => AttributeKind::Used { used_by: UsedBy::Linker },
(Some(_), _, _) => AttributeKind::Used { used_by: UsedBy::Compiler },
(_, _, Some(_)) => AttributeKind::Used { used_by: UsedBy::Default },
(None, None, None) => return None,
})
}
Expand Down
34 changes: 9 additions & 25 deletions compiler/rustc_attr_parsing/src/attributes/crate_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ impl SingleAttributeParser for RecursionLimitParser {
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
let nv = cx.expect_name_value(args, cx.attr_span, None)?;

Some(AttributeKind::RecursionLimit {
limit: cx.parse_limit_int(nv)?,
attr_span: cx.attr_span,
limit_span: nv.value_span,
})
Some(AttributeKind::RecursionLimit { limit: cx.parse_limit_int(nv)? })
}
}

Expand All @@ -102,11 +98,7 @@ impl SingleAttributeParser for MoveSizeLimitParser {
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
let nv = cx.expect_name_value(args, cx.attr_span, None)?;

Some(AttributeKind::MoveSizeLimit {
limit: cx.parse_limit_int(nv)?,
attr_span: cx.attr_span,
limit_span: nv.value_span,
})
Some(AttributeKind::MoveSizeLimit { limit: cx.parse_limit_int(nv)? })
}
}

Expand All @@ -121,11 +113,7 @@ impl SingleAttributeParser for TypeLengthLimitParser {
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
let nv = cx.expect_name_value(args, cx.attr_span, None)?;

Some(AttributeKind::TypeLengthLimit {
limit: cx.parse_limit_int(nv)?,
attr_span: cx.attr_span,
limit_span: nv.value_span,
})
Some(AttributeKind::TypeLengthLimit { limit: cx.parse_limit_int(nv)? })
}
}

Expand All @@ -139,11 +127,7 @@ impl SingleAttributeParser for PatternComplexityLimitParser {
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
let nv = cx.expect_name_value(args, cx.attr_span, None)?;

Some(AttributeKind::PatternComplexityLimit {
limit: cx.parse_limit_int(nv)?,
attr_span: cx.attr_span,
limit_span: nv.value_span,
})
Some(AttributeKind::PatternComplexityLimit { limit: cx.parse_limit_int(nv)? })
}
}

Expand All @@ -152,7 +136,7 @@ pub(crate) struct NoCoreParser;
impl NoArgsAttributeParser for NoCoreParser {
const PATH: &[Symbol] = &[sym::no_core];
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoCore;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoCore;
}

pub(crate) struct NoStdParser;
Expand All @@ -161,7 +145,7 @@ impl NoArgsAttributeParser for NoStdParser {
const PATH: &[Symbol] = &[sym::no_std];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoStd;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoStd;
}

pub(crate) struct NoMainParser;
Expand All @@ -178,7 +162,7 @@ pub(crate) struct RustcCoherenceIsCoreParser;
impl NoArgsAttributeParser for RustcCoherenceIsCoreParser {
const PATH: &[Symbol] = &[sym::rustc_coherence_is_core];
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcCoherenceIsCore;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcCoherenceIsCore;
}

pub(crate) struct WindowsSubsystemParser;
Expand All @@ -204,7 +188,7 @@ impl SingleAttributeParser for WindowsSubsystemParser {
}
};

Some(AttributeKind::WindowsSubsystem(kind, cx.attr_span))
Some(AttributeKind::WindowsSubsystem(kind))
}
}

Expand Down Expand Up @@ -314,7 +298,7 @@ pub(crate) struct RegisterToolParser;
impl CombineAttributeParser for RegisterToolParser {
const PATH: &[Symbol] = &[sym::register_tool];
type Item = Ident;
const CONVERT: ConvertFn<Self::Item> = AttributeKind::RegisterTool;
const CONVERT: ConvertFn<Self::Item> = |tools, _span| AttributeKind::RegisterTool(tools);
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
const TEMPLATE: AttributeTemplate = template!(List: &["tool1, tool2, ..."]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ impl SingleAttributeParser for DoNotRecommendParser {
return None;
}

Some(AttributeKind::DoNotRecommend { attr_span })
Some(AttributeKind::DoNotRecommend)
}
}
38 changes: 16 additions & 22 deletions compiler/rustc_attr_parsing/src/attributes/diagnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::ops::Range;
use rustc_errors::E0232;
use rustc_hir::AttrPath;
use rustc_hir::attrs::diagnostic::{
Directive, FilterFormatString, Flag, FormatArg, FormatString, LitOrArg, Name, NameValue,
OnUnimplementedCondition, Piece, Predicate,
Directive, Filter, FilterFormatString, Flag, FormatArg, FormatString, LitOrArg, Name,
NameValue, Piece, Predicate,
};
use rustc_macros::Diagnostic;
use rustc_parse_format::{
Expand Down Expand Up @@ -201,12 +201,11 @@ fn parse_directive_items<'p>(
items: impl Iterator<Item = &'p MetaItemOrLitParser>,
is_root: bool,
) -> Option<Directive> {
let condition = None;
let mut message: Option<(Span, _)> = None;
let mut label: Option<(Span, _)> = None;
let mut notes = ThinVec::new();
let mut parent_label = None;
let mut subcommands = ThinVec::new();
let mut filters = ThinVec::new();

for item in items {
let span = item.span();
Expand Down Expand Up @@ -330,29 +329,27 @@ fn parse_directive_items<'p>(
if is_root {
let items = or_malformed!(item.args().as_list()?);
let mut iter = items.mixed();
let condition: &MetaItemOrLitParser = match iter.next() {
let filter: &MetaItemOrLitParser = match iter.next() {
Some(c) => c,
None => {
cx.emit_err(InvalidOnClause::Empty { span });
continue;
}
};

let condition = parse_condition(condition);
let filter = parse_filter(filter);

if items.len() < 2 {
// Something like `#[rustc_on_unimplemented(on(.., /* nothing */))]`
// There's a condition but no directive behind it, this is a mistake.
// There's a filter but no directive behind it, this is a mistake.
malformed!();
}

let mut directive =
or_malformed!(parse_directive_items(cx, mode, iter, false)?);

match condition {
Ok(c) => {
directive.condition = Some(c);
subcommands.push(directive);
match filter {
Ok(filter) => {
let directive =
or_malformed!(parse_directive_items(cx, mode, iter, false)?);
filters.push((filter, directive));
}
Err(e) => {
cx.emit_err(e);
Expand All @@ -371,8 +368,7 @@ fn parse_directive_items<'p>(

Some(Directive {
is_rustc_attr: matches!(mode, Mode::RustcOnUnimplemented),
condition,
subcommands,
filters,
message,
label,
notes,
Expand Down Expand Up @@ -513,12 +509,10 @@ fn slice_span(input: Span, Range { start, end }: Range<usize>, is_source_literal
if is_source_literal { input.from_inner(InnerSpan { start, end }) } else { input }
}

pub(crate) fn parse_condition(
input: &MetaItemOrLitParser,
) -> Result<OnUnimplementedCondition, InvalidOnClause> {
pub(crate) fn parse_filter(input: &MetaItemOrLitParser) -> Result<Filter, InvalidOnClause> {
let span = input.span();
let pred = parse_predicate(input)?;
Ok(OnUnimplementedCondition { span, pred })
Ok(Filter { span, pred })
}

fn parse_predicate(input: &MetaItemOrLitParser) -> Result<Predicate, InvalidOnClause> {
Expand Down Expand Up @@ -553,7 +547,7 @@ fn parse_predicate(input: &MetaItemOrLitParser) -> Result<Predicate, InvalidOnCl
return Err(InvalidOnClause::UnsupportedLiteral { span: p.args_span() });
};
let name = parse_name(predicate.name);
let value = parse_filter(value.name);
let value = parse_filter_format(value.name);
let kv = NameValue { name, value };
Ok(Predicate::Match(kv))
}
Expand Down Expand Up @@ -588,7 +582,7 @@ fn parse_name(name: Symbol) -> Name {
}
}

fn parse_filter(input: Symbol) -> FilterFormatString {
fn parse_filter_format(input: Symbol) -> FilterFormatString {
let pieces = Parser::new(input.as_str(), None, None, false, ParseMode::Diagnostic)
.map(|p| match p {
RpfPiece::Lit(s) => LitOrArg::Lit(Symbol::intern(s)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl AttributeParser for OnMoveParser {
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);

fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
if let Some(span) = self.span {
Some(AttributeKind::OnMove { span, directive: self.directive.map(|d| Box::new(d.1)) })
if let Some(_span) = self.span {
Some(AttributeKind::OnMove { directive: self.directive.map(|d| Box::new(d.1)) })
} else {
None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ impl AttributeParser for OnUnimplementedParser {
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);

fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
if let Some(span) = self.span {
if let Some(_span) = self.span {
Some(AttributeKind::OnUnimplemented {
span,
directive: self.directive.map(|d| Box::new(d.1)),
})
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ impl AttributeParser for OnUnknownParser {
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);

fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
if let Some(span) = self.span {
Some(AttributeKind::OnUnknown {
span,
directive: self.directive.map(|d| Box::new(d.1)),
})
if let Some(_span) = self.span {
Some(AttributeKind::OnUnknown { directive: self.directive.map(|d| Box::new(d.1)) })
} else {
None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ impl AttributeParser for OnUnmatchArgsParser {
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);

fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
if let Some(span) = self.span {
Some(AttributeKind::OnUnmatchArgs {
span,
directive: self.directive.map(|d| Box::new(d.1)),
})
if let Some(_span) = self.span {
Some(AttributeKind::OnUnmatchArgs { directive: self.directive.map(|d| Box::new(d.1)) })
} else {
None
}
Expand Down
Loading
Loading