Skip to content

Commit e52f547

Browse files
committed
Auto merge of #154160 - JonathanBrouwer:rollup-4jbkEbt, r=JonathanBrouwer
Rollup of 6 pull requests Successful merges: - #154154 (Emit fewer errors for incorrect rtn and `=` -> `:` typos in bindings) - #154155 (tests/ui/async-await/drop-option-future.rs: New regression test) - #146961 (Allow passing `expr` metavariable as `cfg` predicate) - #154118 (don't suggest non-deriveable traits for unions) - #154120 (Start migrating `DecorateDiagCompat::Builtin` items to `DecorateDiagCompat::Dynamic`) - #154156 (Moving issue-52049 to borrowck)
2 parents 44e6620 + 6d2a545 commit e52f547

File tree

47 files changed

+1083
-349
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1083
-349
lines changed

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::Arc;
22

33
use rustc_ast::{self as ast, *};
4+
use rustc_errors::StashKey;
45
use rustc_hir::def::{DefKind, PartialRes, PerNS, Res};
56
use rustc_hir::def_id::DefId;
67
use rustc_hir::{self as hir, GenericArg};
@@ -298,7 +299,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
298299
sym::return_type_notation,
299300
);
300301
}
301-
err.emit();
302+
err.stash(path_span, StashKey::ReturnTypeNotation);
302303
(
303304
GenericArgsCtor {
304305
args: Default::default(),

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
2020
use thin_vec::ThinVec;
2121

2222
use crate::context::{AcceptContext, ShouldEmit, Stage};
23-
use crate::parser::{ArgParser, MetaItemListParser, MetaItemOrLitParser, NameValueParser};
23+
use crate::parser::{
24+
AllowExprMetavar, ArgParser, MetaItemListParser, MetaItemOrLitParser, NameValueParser,
25+
};
2426
use crate::session_diagnostics::{
2527
AttributeParseError, AttributeParseErrorReason, CfgAttrBadDelim, MetaBadDelimSugg,
2628
ParsedDescription,
@@ -363,6 +365,7 @@ fn parse_cfg_attr_internal<'a>(
363365
let meta = MetaItemOrLitParser::parse_single(
364366
parser,
365367
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
368+
AllowExprMetavar::Yes,
366369
)?;
367370
let pred_span = pred_start.with_hi(parser.token.span.hi());
368371

compiler/rustc_attr_parsing/src/attributes/cfg_select.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_session::lint::BuiltinLintDiag;
1212
use rustc_session::lint::builtin::UNREACHABLE_CFG_SELECT_PREDICATES;
1313
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
1414

15-
use crate::parser::MetaItemOrLitParser;
15+
use crate::parser::{AllowExprMetavar, MetaItemOrLitParser};
1616
use crate::{AttributeParser, ParsedDescription, ShouldEmit, parse_cfg_entry};
1717

1818
#[derive(Clone)]
@@ -94,6 +94,7 @@ pub fn parse_cfg_select(
9494
let meta = MetaItemOrLitParser::parse_single(
9595
p,
9696
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
97+
AllowExprMetavar::Yes,
9798
)
9899
.map_err(|diag| diag.emit())?;
99100
let cfg_span = meta.span();

compiler/rustc_attr_parsing/src/attributes/diagnostic/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,12 @@ fn parse_filter(input: Symbol) -> FilterFormatString {
470470
// if the integer type has been resolved, to allow targeting all integers.
471471
// `"{integer}"` and `"{float}"` come from numerics that haven't been inferred yet,
472472
// from the `Display` impl of `InferTy` to be precise.
473+
// `"{union|enum|struct}"` is used as a special selector for ADTs.
473474
//
474475
// Don't try to format these later!
475-
Position::ArgumentNamed(arg @ ("integer" | "integral" | "float")) => {
476-
LitOrArg::Lit(Symbol::intern(&format!("{{{arg}}}")))
477-
}
476+
Position::ArgumentNamed(
477+
arg @ ("integer" | "integral" | "float" | "union" | "enum" | "struct"),
478+
) => LitOrArg::Lit(Symbol::intern(&format!("{{{arg}}}"))),
478479

479480
Position::ArgumentNamed(arg) => LitOrArg::Arg(Symbol::intern(arg)),
480481
Position::ArgumentImplicitlyIs(_) => LitOrArg::Lit(sym::empty_braces),

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_span::{DUMMY_SP, Span, Symbol, sym};
1414

1515
use crate::context::{AcceptContext, FinalizeContext, FinalizeFn, SharedContext, Stage};
1616
use crate::early_parsed::{EARLY_PARSED_ATTRIBUTES, EarlyParsedState};
17-
use crate::parser::{ArgParser, PathParser, RefPathParser};
17+
use crate::parser::{AllowExprMetavar, ArgParser, PathParser, RefPathParser};
1818
use crate::session_diagnostics::ParsedDescription;
1919
use crate::{Early, Late, OmitDoc, ShouldEmit};
2020

@@ -139,6 +139,7 @@ impl<'sess> AttributeParser<'sess, Early> {
139139
emit_errors: ShouldEmit,
140140
parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser) -> Option<T>,
141141
template: &AttributeTemplate,
142+
allow_expr_metavar: AllowExprMetavar,
142143
) -> Option<T> {
143144
let ast::AttrKind::Normal(normal_attr) = &attr.kind else {
144145
panic!("parse_single called on a doc attr")
@@ -152,6 +153,7 @@ impl<'sess> AttributeParser<'sess, Early> {
152153
&parts,
153154
&sess.psess,
154155
emit_errors,
156+
allow_expr_metavar,
155157
)?;
156158
Self::parse_single_args(
157159
sess,
@@ -333,6 +335,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
333335
&parts,
334336
&self.sess.psess,
335337
self.stage.should_emit(),
338+
AllowExprMetavar::No,
336339
) else {
337340
continue;
338341
};

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl ArgParser {
109109
parts: &[Symbol],
110110
psess: &'sess ParseSess,
111111
should_emit: ShouldEmit,
112+
allow_expr_metavar: AllowExprMetavar,
112113
) -> Option<Self> {
113114
Some(match value {
114115
AttrArgs::Empty => Self::NoArgs,
@@ -122,6 +123,7 @@ impl ArgParser {
122123
args.dspan.entire(),
123124
psess,
124125
ShouldEmit::ErrorsAndLints { recovery: Recovery::Forbidden },
126+
allow_expr_metavar,
125127
) {
126128
Ok(p) => return Some(ArgParser::List(p)),
127129
Err(e) => {
@@ -147,9 +149,15 @@ impl ArgParser {
147149
}
148150

149151
Self::List(
150-
MetaItemListParser::new(&args.tokens, args.dspan.entire(), psess, should_emit)
151-
.map_err(|e| should_emit.emit_err(e))
152-
.ok()?,
152+
MetaItemListParser::new(
153+
&args.tokens,
154+
args.dspan.entire(),
155+
psess,
156+
should_emit,
157+
allow_expr_metavar,
158+
)
159+
.map_err(|e| should_emit.emit_err(e))
160+
.ok()?,
153161
)
154162
}
155163
AttrArgs::Eq { eq_span, expr } => Self::NameValue(NameValueParser {
@@ -217,8 +225,9 @@ impl MetaItemOrLitParser {
217225
pub fn parse_single<'sess>(
218226
parser: &mut Parser<'sess>,
219227
should_emit: ShouldEmit,
228+
allow_expr_metavar: AllowExprMetavar,
220229
) -> PResult<'sess, MetaItemOrLitParser> {
221-
let mut this = MetaItemListParserContext { parser, should_emit };
230+
let mut this = MetaItemListParserContext { parser, should_emit, allow_expr_metavar };
222231
this.parse_meta_item_inner()
223232
}
224233

@@ -404,9 +413,19 @@ fn expr_to_lit<'sess>(
404413
}
405414
}
406415

416+
/// Whether expansions of `expr` metavariables from decrarative macros
417+
/// are permitted. Used when parsing meta items; currently, only `cfg` predicates
418+
/// enable this option
419+
#[derive(Clone, Copy, PartialEq, Eq)]
420+
pub enum AllowExprMetavar {
421+
No,
422+
Yes,
423+
}
424+
407425
struct MetaItemListParserContext<'a, 'sess> {
408426
parser: &'a mut Parser<'sess>,
409427
should_emit: ShouldEmit,
428+
allow_expr_metavar: AllowExprMetavar,
410429
}
411430

412431
impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
@@ -447,20 +466,44 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
447466
Ok(lit)
448467
}
449468

450-
fn parse_attr_item(&mut self) -> PResult<'sess, MetaItemParser> {
451-
if let Some(MetaVarKind::Meta { has_meta_form }) = self.parser.token.is_metavar_seq() {
452-
return if has_meta_form {
453-
let attr_item = self
454-
.parser
455-
.eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
456-
MetaItemListParserContext { parser: this, should_emit: self.should_emit }
457-
.parse_attr_item()
458-
})
459-
.unwrap();
460-
Ok(attr_item)
461-
} else {
462-
self.parser.unexpected_any()
463-
};
469+
fn parse_meta_item(&mut self) -> PResult<'sess, MetaItemParser> {
470+
if let Some(metavar) = self.parser.token.is_metavar_seq() {
471+
match (metavar, self.allow_expr_metavar) {
472+
(kind @ MetaVarKind::Expr { .. }, AllowExprMetavar::Yes) => {
473+
return self
474+
.parser
475+
.eat_metavar_seq(kind, |this| {
476+
MetaItemListParserContext {
477+
parser: this,
478+
should_emit: self.should_emit,
479+
allow_expr_metavar: AllowExprMetavar::Yes,
480+
}
481+
.parse_meta_item()
482+
})
483+
.ok_or_else(|| {
484+
self.parser.unexpected_any::<core::convert::Infallible>().unwrap_err()
485+
});
486+
}
487+
(MetaVarKind::Meta { has_meta_form }, _) => {
488+
return if has_meta_form {
489+
let attr_item = self
490+
.parser
491+
.eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
492+
MetaItemListParserContext {
493+
parser: this,
494+
should_emit: self.should_emit,
495+
allow_expr_metavar: self.allow_expr_metavar,
496+
}
497+
.parse_meta_item()
498+
})
499+
.unwrap();
500+
Ok(attr_item)
501+
} else {
502+
self.parser.unexpected_any()
503+
};
504+
}
505+
_ => {}
506+
}
464507
}
465508

466509
let path = self.parser.parse_path(PathStyle::Mod)?;
@@ -469,8 +512,12 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
469512
let args = if self.parser.check(exp!(OpenParen)) {
470513
let start = self.parser.token.span;
471514
let (sub_parsers, _) = self.parser.parse_paren_comma_seq(|parser| {
472-
MetaItemListParserContext { parser, should_emit: self.should_emit }
473-
.parse_meta_item_inner()
515+
MetaItemListParserContext {
516+
parser,
517+
should_emit: self.should_emit,
518+
allow_expr_metavar: self.allow_expr_metavar,
519+
}
520+
.parse_meta_item_inner()
474521
})?;
475522
let end = self.parser.prev_token.span;
476523
ArgParser::List(MetaItemListParser { sub_parsers, span: start.with_hi(end.hi()) })
@@ -492,7 +539,7 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
492539
Ok(MetaItemOrLitParser::Lit(self.unsuffixed_meta_item_from_lit(token_lit)?))
493540
} else {
494541
let prev_pros = self.parser.approx_token_stream_pos();
495-
match self.parse_attr_item() {
542+
match self.parse_meta_item() {
496543
Ok(item) => Ok(MetaItemOrLitParser::MetaItemParser(item)),
497544
Err(err) => {
498545
// If `parse_attr_item` made any progress, it likely has a more precise error we should prefer
@@ -580,13 +627,15 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
580627
psess: &'sess ParseSess,
581628
span: Span,
582629
should_emit: ShouldEmit,
630+
allow_expr_metavar: AllowExprMetavar,
583631
) -> PResult<'sess, MetaItemListParser> {
584632
let mut parser = Parser::new(psess, tokens, None);
585633
if let ShouldEmit::ErrorsAndLints { recovery } = should_emit {
586634
parser = parser.recovery(recovery);
587635
}
588636

589-
let mut this = MetaItemListParserContext { parser: &mut parser, should_emit };
637+
let mut this =
638+
MetaItemListParserContext { parser: &mut parser, should_emit, allow_expr_metavar };
590639

591640
// Presumably, the majority of the time there will only be one attr.
592641
let mut sub_parsers = ThinVec::with_capacity(1);
@@ -618,8 +667,15 @@ impl MetaItemListParser {
618667
span: Span,
619668
psess: &'sess ParseSess,
620669
should_emit: ShouldEmit,
670+
allow_expr_metavar: AllowExprMetavar,
621671
) -> Result<Self, Diag<'sess>> {
622-
MetaItemListParserContext::parse(tokens.clone(), psess, span, should_emit)
672+
MetaItemListParserContext::parse(
673+
tokens.clone(),
674+
psess,
675+
span,
676+
should_emit,
677+
allow_expr_metavar,
678+
)
623679
}
624680

625681
/// Lets you pick and choose as what you want to parse each element in the list

compiler/rustc_builtin_macros/src/cfg.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
55
use rustc_ast::tokenstream::TokenStream;
66
use rustc_ast::{AttrStyle, token};
7-
use rustc_attr_parsing as attr;
8-
use rustc_attr_parsing::parser::MetaItemOrLitParser;
7+
use rustc_attr_parsing::parser::{AllowExprMetavar, MetaItemOrLitParser};
98
use rustc_attr_parsing::{
10-
AttributeParser, CFG_TEMPLATE, ParsedDescription, ShouldEmit, parse_cfg_entry,
9+
self as attr, AttributeParser, CFG_TEMPLATE, ParsedDescription, ShouldEmit, parse_cfg_entry,
1110
};
1211
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
1312
use rustc_hir::attrs::CfgEntry;
@@ -44,6 +43,7 @@ fn parse_cfg(cx: &ExtCtxt<'_>, span: Span, tts: TokenStream) -> Result<CfgEntry,
4443
let meta = MetaItemOrLitParser::parse_single(
4544
&mut parser,
4645
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
46+
AllowExprMetavar::Yes,
4747
)
4848
.map_err(|diag| diag.emit())?;
4949
let cfg = AttributeParser::parse_single_args(

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ pub enum StashKey {
386386
/// it's a method call without parens. If later on in `hir_typeck` we find out that this is
387387
/// the case we suppress this message and we give a better suggestion.
388388
GenericInFieldExpr,
389+
ReturnTypeNotation,
389390
}
390391

391392
fn default_track_diagnostic<R>(diag: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {

compiler/rustc_expand/src/config.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use rustc_ast::{
1010
self as ast, AttrItemKind, AttrKind, AttrStyle, Attribute, DUMMY_NODE_ID, EarlyParsedAttribute,
1111
HasAttrs, HasTokens, MetaItem, MetaItemInner, NodeId, NormalAttr,
1212
};
13-
use rustc_attr_parsing as attr;
13+
use rustc_attr_parsing::parser::AllowExprMetavar;
1414
use rustc_attr_parsing::{
15-
AttributeParser, CFG_TEMPLATE, EvalConfigResult, ShouldEmit, eval_config_entry, parse_cfg,
15+
self as attr, AttributeParser, CFG_TEMPLATE, EvalConfigResult, ShouldEmit, eval_config_entry,
16+
parse_cfg,
1617
};
1718
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1819
use rustc_errors::msg;
@@ -402,6 +403,7 @@ impl<'a> StripUnconfigured<'a> {
402403
emit_errors,
403404
parse_cfg,
404405
&CFG_TEMPLATE,
406+
AllowExprMetavar::Yes,
405407
) else {
406408
// Cfg attribute was not parsable, give up
407409
return EvalConfigResult::True;

compiler/rustc_expand/src/expand.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_ast::{
1313
TyKind, token,
1414
};
1515
use rustc_ast_pretty::pprust;
16+
use rustc_attr_parsing::parser::AllowExprMetavar;
1617
use rustc_attr_parsing::{
1718
AttributeParser, CFG_TEMPLATE, Early, EvalConfigResult, ShouldEmit, eval_config_entry,
1819
parse_cfg, validate_attr,
@@ -2224,6 +2225,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22242225
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
22252226
parse_cfg,
22262227
&CFG_TEMPLATE,
2228+
AllowExprMetavar::Yes,
22272229
) else {
22282230
// Cfg attribute was not parsable, give up
22292231
return EvalConfigResult::True;

0 commit comments

Comments
 (0)