Skip to content

Commit 5f9dd05

Browse files
committed
Auto merge of rust-lang#148456 - matthiaskrgr:rollup-9drvuel, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#147141 (Suggest making binding `mut` on `&mut` reborrow) - rust-lang#147945 (Port `cfg!()` macro to the new attribute parsing system ) - rust-lang#147951 (Add check for `+=` typo in let chains) - rust-lang#148004 (fix: Only special case single line item attribute suggestions) - rust-lang#148264 (reflect that type and const parameter can be intermixed) - rust-lang#148363 (Fix `wasm_import_module` attribute cross-crate) - rust-lang#148447 (Tweak E0401) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 20383c9 + bcf227a commit 5f9dd05

File tree

65 files changed

+660
-216
lines changed

Some content is hidden

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

65 files changed

+660
-216
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ use rustc_session::config::ExpectedValues;
1212
use rustc_session::lint::BuiltinLintDiag;
1313
use rustc_session::lint::builtin::UNEXPECTED_CFGS;
1414
use rustc_session::parse::{ParseSess, feature_err};
15-
use rustc_span::{Span, Symbol, sym};
15+
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
1616
use thin_vec::ThinVec;
1717

1818
use crate::context::{AcceptContext, ShouldEmit, Stage};
1919
use crate::parser::{ArgParser, MetaItemListParser, MetaItemOrLitParser, NameValueParser};
2020
use crate::session_diagnostics::{
2121
AttributeParseError, AttributeParseErrorReason, CfgAttrBadDelim, MetaBadDelimSugg,
22+
ParsedDescription,
2223
};
2324
use crate::{
2425
AttributeParser, CfgMatchesLintEmitter, fluent_generated, parse_version, session_diagnostics,
@@ -47,20 +48,19 @@ pub fn parse_cfg<'c, S: Stage>(
4748
cx.expected_single_argument(list.span);
4849
return None;
4950
};
50-
parse_cfg_entry(cx, single)
51+
parse_cfg_entry(cx, single).ok()
5152
}
5253

53-
pub(crate) fn parse_cfg_entry<S: Stage>(
54+
pub fn parse_cfg_entry<S: Stage>(
5455
cx: &mut AcceptContext<'_, '_, S>,
5556
item: &MetaItemOrLitParser<'_>,
56-
) -> Option<CfgEntry> {
57-
Some(match item {
57+
) -> Result<CfgEntry, ErrorGuaranteed> {
58+
Ok(match item {
5859
MetaItemOrLitParser::MetaItemParser(meta) => match meta.args() {
5960
ArgParser::List(list) => match meta.path().word_sym() {
6061
Some(sym::not) => {
6162
let Some(single) = list.single() else {
62-
cx.expected_single_argument(list.span);
63-
return None;
63+
return Err(cx.expected_single_argument(list.span));
6464
};
6565
CfgEntry::Not(Box::new(parse_cfg_entry(cx, single)?), list.span)
6666
}
@@ -75,49 +75,47 @@ pub(crate) fn parse_cfg_entry<S: Stage>(
7575
Some(sym::target) => parse_cfg_entry_target(cx, list, meta.span())?,
7676
Some(sym::version) => parse_cfg_entry_version(cx, list, meta.span())?,
7777
_ => {
78-
cx.emit_err(session_diagnostics::InvalidPredicate {
78+
return Err(cx.emit_err(session_diagnostics::InvalidPredicate {
7979
span: meta.span(),
8080
predicate: meta.path().to_string(),
81-
});
82-
return None;
81+
}));
8382
}
8483
},
8584
a @ (ArgParser::NoArgs | ArgParser::NameValue(_)) => {
8685
let Some(name) = meta.path().word_sym() else {
87-
cx.expected_identifier(meta.path().span());
88-
return None;
86+
return Err(cx.expected_identifier(meta.path().span()));
8987
};
9088
parse_name_value(name, meta.path().span(), a.name_value(), meta.span(), cx)?
9189
}
9290
},
9391
MetaItemOrLitParser::Lit(lit) => match lit.kind {
9492
LitKind::Bool(b) => CfgEntry::Bool(b, lit.span),
95-
_ => {
96-
cx.expected_identifier(lit.span);
97-
return None;
98-
}
93+
_ => return Err(cx.expected_identifier(lit.span)),
9994
},
100-
MetaItemOrLitParser::Err(_, _) => return None,
95+
MetaItemOrLitParser::Err(_, err) => return Err(*err),
10196
})
10297
}
10398

10499
fn parse_cfg_entry_version<S: Stage>(
105100
cx: &mut AcceptContext<'_, '_, S>,
106101
list: &MetaItemListParser<'_>,
107102
meta_span: Span,
108-
) -> Option<CfgEntry> {
103+
) -> Result<CfgEntry, ErrorGuaranteed> {
109104
try_gate_cfg(sym::version, meta_span, cx.sess(), cx.features_option());
110105
let Some(version) = list.single() else {
111-
cx.emit_err(session_diagnostics::ExpectedSingleVersionLiteral { span: list.span });
112-
return None;
106+
return Err(
107+
cx.emit_err(session_diagnostics::ExpectedSingleVersionLiteral { span: list.span })
108+
);
113109
};
114110
let Some(version_lit) = version.lit() else {
115-
cx.emit_err(session_diagnostics::ExpectedVersionLiteral { span: version.span() });
116-
return None;
111+
return Err(
112+
cx.emit_err(session_diagnostics::ExpectedVersionLiteral { span: version.span() })
113+
);
117114
};
118115
let Some(version_str) = version_lit.value_str() else {
119-
cx.emit_err(session_diagnostics::ExpectedVersionLiteral { span: version_lit.span });
120-
return None;
116+
return Err(
117+
cx.emit_err(session_diagnostics::ExpectedVersionLiteral { span: version_lit.span })
118+
);
121119
};
122120

123121
let min_version = parse_version(version_str).or_else(|| {
@@ -127,14 +125,14 @@ fn parse_cfg_entry_version<S: Stage>(
127125
None
128126
});
129127

130-
Some(CfgEntry::Version(min_version, list.span))
128+
Ok(CfgEntry::Version(min_version, list.span))
131129
}
132130

133131
fn parse_cfg_entry_target<S: Stage>(
134132
cx: &mut AcceptContext<'_, '_, S>,
135133
list: &MetaItemListParser<'_>,
136134
meta_span: Span,
137-
) -> Option<CfgEntry> {
135+
) -> Result<CfgEntry, ErrorGuaranteed> {
138136
if let Some(features) = cx.features_option()
139137
&& !features.cfg_target_compact()
140138
{
@@ -161,17 +159,16 @@ fn parse_cfg_entry_target<S: Stage>(
161159

162160
// Then, parse it as a name-value item
163161
let Some(name) = sub_item.path().word_sym() else {
164-
cx.expected_identifier(sub_item.path().span());
165-
return None;
162+
return Err(cx.expected_identifier(sub_item.path().span()));
166163
};
167164
let name = Symbol::intern(&format!("target_{name}"));
168-
if let Some(cfg) =
165+
if let Ok(cfg) =
169166
parse_name_value(name, sub_item.path().span(), Some(nv), sub_item.span(), cx)
170167
{
171168
result.push(cfg);
172169
}
173170
}
174-
Some(CfgEntry::All(result, list.span))
171+
Ok(CfgEntry::All(result, list.span))
175172
}
176173

177174
fn parse_name_value<S: Stage>(
@@ -180,21 +177,22 @@ fn parse_name_value<S: Stage>(
180177
value: Option<&NameValueParser>,
181178
span: Span,
182179
cx: &mut AcceptContext<'_, '_, S>,
183-
) -> Option<CfgEntry> {
180+
) -> Result<CfgEntry, ErrorGuaranteed> {
184181
try_gate_cfg(name, span, cx.sess(), cx.features_option());
185182

186183
let value = match value {
187184
None => None,
188185
Some(value) => {
189186
let Some(value_str) = value.value_as_str() else {
190-
cx.expected_string_literal(value.value_span, Some(value.value_as_lit()));
191-
return None;
187+
return Err(
188+
cx.expected_string_literal(value.value_span, Some(value.value_as_lit()))
189+
);
192190
};
193191
Some((value_str, value.value_span))
194192
}
195193
};
196194

197-
Some(CfgEntry::NameValue { name, name_span, value, span })
195+
Ok(CfgEntry::NameValue { name, name_span, value, span })
198196
}
199197

200198
pub fn eval_config_entry(
@@ -355,7 +353,8 @@ pub fn parse_cfg_attr(
355353
span,
356354
attr_span: cfg_attr.span,
357355
template: CFG_ATTR_TEMPLATE,
358-
attribute: AttrPath::from_ast(&cfg_attr.get_normal_item().path),
356+
path: AttrPath::from_ast(&cfg_attr.get_normal_item().path),
357+
description: ParsedDescription::Attribute,
359358
reason,
360359
suggestions: CFG_ATTR_TEMPLATE.suggestions(Some(cfg_attr.style), sym::cfg_attr),
361360
});
@@ -398,6 +397,7 @@ fn parse_cfg_attr_internal<'a>(
398397
.into_boxed_slice(),
399398
span: attribute.span,
400399
},
400+
ParsedDescription::Attribute,
401401
pred_span,
402402
CRATE_NODE_ID,
403403
features,
@@ -406,7 +406,8 @@ fn parse_cfg_attr_internal<'a>(
406406
parse_cfg_entry,
407407
&CFG_ATTR_TEMPLATE,
408408
)
409-
.ok_or_else(|| {
409+
.map_err(|_err: ErrorGuaranteed| {
410+
// We have an `ErrorGuaranteed` so this delayed bug cannot fail, but we need a `Diag` for the `PResult` so we make one anyways
410411
let mut diag = sess.dcx().struct_err(
411412
"cfg_entry parsing failing with `ShouldEmit::ErrorsAndLints` should emit a error.",
412413
);

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ impl LinkParser {
396396
)
397397
.emit();
398398
}
399-
*cfg = parse_cfg_entry(cx, link_cfg);
399+
*cfg = parse_cfg_entry(cx, link_cfg).ok();
400400
true
401401
}
402402

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ use crate::attributes::traits::{
7171
use crate::attributes::transparency::TransparencyParser;
7272
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
7373
use crate::parser::{ArgParser, PathParser};
74-
use crate::session_diagnostics::{AttributeParseError, AttributeParseErrorReason, UnknownMetaItem};
74+
use crate::session_diagnostics::{
75+
AttributeParseError, AttributeParseErrorReason, ParsedDescription, UnknownMetaItem,
76+
};
7577
use crate::target_checking::AllowedTargets;
7678

7779
type GroupType<S> = LazyLock<GroupTypeInner<S>>;
@@ -353,6 +355,10 @@ pub struct AcceptContext<'f, 'sess, S: Stage> {
353355
/// Whether it is an inner or outer attribute
354356
pub(crate) attr_style: AttrStyle,
355357

358+
/// A description of the thing we are parsing using this attribute parser
359+
/// We are not only using these parsers for attributes, but also for macros such as the `cfg!()` macro.
360+
pub(crate) parsed_description: ParsedDescription,
361+
356362
/// The expected structure of the attribute.
357363
///
358364
/// Used in reporting errors to give a hint to users what the attribute *should* look like.
@@ -431,7 +437,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
431437
span,
432438
attr_span: self.attr_span,
433439
template: self.template.clone(),
434-
attribute: self.attr_path.clone(),
440+
path: self.attr_path.clone(),
441+
description: self.parsed_description,
435442
reason: AttributeParseErrorReason::ExpectedStringLiteral {
436443
byte_string: actual_literal.and_then(|i| {
437444
i.kind.is_bytestr().then(|| self.sess().source_map().start_point(i.span))
@@ -446,7 +453,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
446453
span,
447454
attr_span: self.attr_span,
448455
template: self.template.clone(),
449-
attribute: self.attr_path.clone(),
456+
path: self.attr_path.clone(),
457+
description: self.parsed_description,
450458
reason: AttributeParseErrorReason::ExpectedIntegerLiteral,
451459
suggestions: self.suggestions(),
452460
})
@@ -457,7 +465,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
457465
span,
458466
attr_span: self.attr_span,
459467
template: self.template.clone(),
460-
attribute: self.attr_path.clone(),
468+
path: self.attr_path.clone(),
469+
description: self.parsed_description,
461470
reason: AttributeParseErrorReason::ExpectedList,
462471
suggestions: self.suggestions(),
463472
})
@@ -468,7 +477,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
468477
span: args_span,
469478
attr_span: self.attr_span,
470479
template: self.template.clone(),
471-
attribute: self.attr_path.clone(),
480+
path: self.attr_path.clone(),
481+
description: self.parsed_description,
472482
reason: AttributeParseErrorReason::ExpectedNoArgs,
473483
suggestions: self.suggestions(),
474484
})
@@ -480,7 +490,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
480490
span,
481491
attr_span: self.attr_span,
482492
template: self.template.clone(),
483-
attribute: self.attr_path.clone(),
493+
path: self.attr_path.clone(),
494+
description: self.parsed_description,
484495
reason: AttributeParseErrorReason::ExpectedIdentifier,
485496
suggestions: self.suggestions(),
486497
})
@@ -493,7 +504,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
493504
span,
494505
attr_span: self.attr_span,
495506
template: self.template.clone(),
496-
attribute: self.attr_path.clone(),
507+
path: self.attr_path.clone(),
508+
description: self.parsed_description,
497509
reason: AttributeParseErrorReason::ExpectedNameValue(name),
498510
suggestions: self.suggestions(),
499511
})
@@ -505,7 +517,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
505517
span,
506518
attr_span: self.attr_span,
507519
template: self.template.clone(),
508-
attribute: self.attr_path.clone(),
520+
path: self.attr_path.clone(),
521+
description: self.parsed_description,
509522
reason: AttributeParseErrorReason::DuplicateKey(key),
510523
suggestions: self.suggestions(),
511524
})
@@ -518,7 +531,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
518531
span,
519532
attr_span: self.attr_span,
520533
template: self.template.clone(),
521-
attribute: self.attr_path.clone(),
534+
path: self.attr_path.clone(),
535+
description: self.parsed_description,
522536
reason: AttributeParseErrorReason::UnexpectedLiteral,
523537
suggestions: self.suggestions(),
524538
})
@@ -529,7 +543,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
529543
span,
530544
attr_span: self.attr_span,
531545
template: self.template.clone(),
532-
attribute: self.attr_path.clone(),
546+
path: self.attr_path.clone(),
547+
description: self.parsed_description,
533548
reason: AttributeParseErrorReason::ExpectedSingleArgument,
534549
suggestions: self.suggestions(),
535550
})
@@ -540,7 +555,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
540555
span,
541556
attr_span: self.attr_span,
542557
template: self.template.clone(),
543-
attribute: self.attr_path.clone(),
558+
path: self.attr_path.clone(),
559+
description: self.parsed_description,
544560
reason: AttributeParseErrorReason::ExpectedAtLeastOneArgument,
545561
suggestions: self.suggestions(),
546562
})
@@ -556,7 +572,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
556572
span,
557573
attr_span: self.attr_span,
558574
template: self.template.clone(),
559-
attribute: self.attr_path.clone(),
575+
path: self.attr_path.clone(),
576+
description: self.parsed_description,
560577
reason: AttributeParseErrorReason::ExpectedSpecificArgument {
561578
possibilities,
562579
strings: false,
@@ -577,7 +594,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
577594
span,
578595
attr_span: self.attr_span,
579596
template: self.template.clone(),
580-
attribute: self.attr_path.clone(),
597+
path: self.attr_path.clone(),
598+
description: self.parsed_description,
581599
reason: AttributeParseErrorReason::ExpectedSpecificArgument {
582600
possibilities,
583601
strings: false,
@@ -597,7 +615,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
597615
span,
598616
attr_span: self.attr_span,
599617
template: self.template.clone(),
600-
attribute: self.attr_path.clone(),
618+
path: self.attr_path.clone(),
619+
description: self.parsed_description,
601620
reason: AttributeParseErrorReason::ExpectedSpecificArgument {
602621
possibilities,
603622
strings: true,

0 commit comments

Comments
 (0)