Skip to content

Commit 4176766

Browse files
Auto merge of #145545 - jhpratt:manual-rollup, r=<try>
Manual rollup of 19 pull requests
2 parents 425a9c0 + cbc4330 commit 4176766

File tree

79 files changed

+784
-512
lines changed

Some content is hidden

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

79 files changed

+784
-512
lines changed

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
5454
Allow(Target::TyAlias),
5555
Allow(Target::Use),
5656
Allow(Target::ForeignFn),
57+
Allow(Target::ForeignStatic),
58+
Allow(Target::ForeignTy),
5759
Allow(Target::Field),
5860
Allow(Target::Trait),
5961
Allow(Target::AssocTy),

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
6262
}
6363
}
6464
ArgParser::NameValue(_) => {
65-
let suggestions =
66-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "inline");
65+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
66+
.suggestions(cx.attr_style, "inline");
6767
let span = cx.attr_span;
6868
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
6969
return None;

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser {
107107
}
108108
}
109109
ArgParser::NameValue(_) => {
110-
let suggestions = MACRO_USE_TEMPLATE.suggestions(false, sym::macro_use);
110+
let suggestions = MACRO_USE_TEMPLATE.suggestions(cx.attr_style, sym::macro_use);
111111
cx.emit_err(session_diagnostics::IllFormedAttributeInputLint {
112112
num_suggestions: suggestions.len(),
113113
suggestions: DiagArgValue::StrListSepByAnd(

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub(crate) mod no_implicit_prelude;
4343
pub(crate) mod non_exhaustive;
4444
pub(crate) mod path;
4545
pub(crate) mod proc_macro_attrs;
46+
pub(crate) mod prototype;
4647
pub(crate) mod repr;
4748
pub(crate) mod rustc_internal;
4849
pub(crate) mod semantics;

compiler/rustc_attr_parsing/src/attributes/must_use.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ impl<S: Stage> SingleAttributeParser<S> for MustUseParser {
3535
Some(value_str)
3636
}
3737
ArgParser::List(_) => {
38-
let suggestions =
39-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "must_use");
38+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
39+
.suggestions(cx.attr_style, "must_use");
4040
cx.emit_err(session_diagnostics::IllFormedAttributeInputLint {
4141
num_suggestions: suggestions.len(),
4242
suggestions: DiagArgValue::StrListSepByAnd(
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//! Attributes that are only used on function prototypes.
2+
3+
use rustc_feature::{AttributeTemplate, template};
4+
use rustc_hir::Target;
5+
use rustc_hir::attrs::{AttributeKind, MirDialect, MirPhase};
6+
use rustc_span::{Span, Symbol, sym};
7+
8+
use super::{AttributeOrder, OnDuplicate};
9+
use crate::attributes::SingleAttributeParser;
10+
use crate::context::{AcceptContext, AllowedTargets, MaybeWarn, Stage};
11+
use crate::parser::ArgParser;
12+
13+
pub(crate) struct CustomMirParser;
14+
15+
impl<S: Stage> SingleAttributeParser<S> for CustomMirParser {
16+
const PATH: &[rustc_span::Symbol] = &[sym::custom_mir];
17+
18+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
19+
20+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
21+
22+
const ALLOWED_TARGETS: AllowedTargets =
23+
AllowedTargets::AllowList(&[MaybeWarn::Allow(Target::Fn)]);
24+
25+
const TEMPLATE: AttributeTemplate = template!(List: &[r#"dialect = "...", phase = "...""#]);
26+
27+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
28+
let Some(list) = args.list() else {
29+
cx.expected_list(cx.attr_span);
30+
return None;
31+
};
32+
33+
let mut dialect = None;
34+
let mut phase = None;
35+
let mut failed = false;
36+
37+
for item in list.mixed() {
38+
let Some(meta_item) = item.meta_item() else {
39+
cx.expected_name_value(item.span(), None);
40+
failed = true;
41+
break;
42+
};
43+
44+
if let Some(arg) = meta_item.word_is(sym::dialect) {
45+
extract_value(cx, sym::dialect, arg, meta_item.span(), &mut dialect, &mut failed);
46+
} else if let Some(arg) = meta_item.word_is(sym::phase) {
47+
extract_value(cx, sym::phase, arg, meta_item.span(), &mut phase, &mut failed);
48+
} else if let Some(word) = meta_item.path().word() {
49+
let word = word.to_string();
50+
cx.unknown_key(meta_item.span(), word, &["dialect", "phase"]);
51+
failed = true;
52+
} else {
53+
cx.expected_name_value(meta_item.span(), None);
54+
failed = true;
55+
};
56+
}
57+
58+
let dialect = parse_dialect(cx, dialect, &mut failed);
59+
let phase = parse_phase(cx, phase, &mut failed);
60+
61+
if failed {
62+
return None;
63+
}
64+
65+
Some(AttributeKind::CustomMir(dialect, phase, cx.attr_span))
66+
}
67+
}
68+
69+
fn extract_value<S: Stage>(
70+
cx: &mut AcceptContext<'_, '_, S>,
71+
key: Symbol,
72+
arg: &ArgParser<'_>,
73+
span: Span,
74+
out_val: &mut Option<(Symbol, Span)>,
75+
failed: &mut bool,
76+
) {
77+
if out_val.is_some() {
78+
cx.duplicate_key(span, key);
79+
*failed = true;
80+
return;
81+
}
82+
83+
let Some(val) = arg.name_value() else {
84+
cx.expected_single_argument(arg.span().unwrap_or(span));
85+
*failed = true;
86+
return;
87+
};
88+
89+
let Some(value_sym) = val.value_as_str() else {
90+
cx.expected_string_literal(val.value_span, Some(val.value_as_lit()));
91+
*failed = true;
92+
return;
93+
};
94+
95+
*out_val = Some((value_sym, val.value_span));
96+
}
97+
98+
fn parse_dialect<S: Stage>(
99+
cx: &mut AcceptContext<'_, '_, S>,
100+
dialect: Option<(Symbol, Span)>,
101+
failed: &mut bool,
102+
) -> Option<(MirDialect, Span)> {
103+
let (dialect, span) = dialect?;
104+
105+
let dialect = match dialect {
106+
sym::analysis => MirDialect::Analysis,
107+
sym::built => MirDialect::Built,
108+
sym::runtime => MirDialect::Runtime,
109+
110+
_ => {
111+
cx.expected_specific_argument(span, vec!["analysis", "built", "runtime"]);
112+
*failed = true;
113+
return None;
114+
}
115+
};
116+
117+
Some((dialect, span))
118+
}
119+
120+
fn parse_phase<S: Stage>(
121+
cx: &mut AcceptContext<'_, '_, S>,
122+
phase: Option<(Symbol, Span)>,
123+
failed: &mut bool,
124+
) -> Option<(MirPhase, Span)> {
125+
let (phase, span) = phase?;
126+
127+
let phase = match phase {
128+
sym::initial => MirPhase::Initial,
129+
sym::post_cleanup => MirPhase::PostCleanup,
130+
sym::optimized => MirPhase::Optimized,
131+
132+
_ => {
133+
cx.expected_specific_argument(span, vec!["initial", "post-cleanup", "optimized"]);
134+
*failed = true;
135+
return None;
136+
}
137+
};
138+
139+
Some((phase, span))
140+
}

compiler/rustc_attr_parsing/src/attributes/test_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
2929
ArgParser::NameValue(name_value) => {
3030
let Some(str_value) = name_value.value_as_str() else {
3131
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
32-
.suggestions(false, "ignore");
32+
.suggestions(cx.attr_style, "ignore");
3333
let span = cx.attr_span;
3434
cx.emit_lint(
3535
AttributeLintKind::IllFormedAttributeInput { suggestions },
@@ -40,8 +40,8 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
4040
Some(str_value)
4141
}
4242
ArgParser::List(_) => {
43-
let suggestions =
44-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "ignore");
43+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
44+
.suggestions(cx.attr_style, "ignore");
4545
let span = cx.attr_span;
4646
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
4747
return None;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::LazyLock;
55

66
use itertools::Itertools;
77
use private::Sealed;
8-
use rustc_ast::{self as ast, LitKind, MetaItemLit, NodeId};
8+
use rustc_ast::{self as ast, AttrStyle, LitKind, MetaItemLit, NodeId};
99
use rustc_errors::{DiagCtxtHandle, Diagnostic};
1010
use rustc_feature::{AttributeTemplate, Features};
1111
use rustc_hir::attrs::AttributeKind;
@@ -46,6 +46,7 @@ use crate::attributes::path::PathParser as PathAttributeParser;
4646
use crate::attributes::proc_macro_attrs::{
4747
ProcMacroAttributeParser, ProcMacroDeriveParser, ProcMacroParser, RustcBuiltinMacroParser,
4848
};
49+
use crate::attributes::prototype::CustomMirParser;
4950
use crate::attributes::repr::{AlignParser, ReprParser};
5051
use crate::attributes::rustc_internal::{
5152
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
@@ -167,6 +168,7 @@ attribute_parsers!(
167168

168169
// tidy-alphabetical-start
169170
Single<CoverageParser>,
171+
Single<CustomMirParser>,
170172
Single<DeprecationParser>,
171173
Single<DummyParser>,
172174
Single<ExportNameParser>,
@@ -313,6 +315,7 @@ pub struct AcceptContext<'f, 'sess, S: Stage> {
313315
/// The span of the attribute currently being parsed
314316
pub(crate) attr_span: Span,
315317

318+
pub(crate) attr_style: AttrStyle,
316319
/// The expected structure of the attribute.
317320
///
318321
/// Used in reporting errors to give a hint to users what the attribute *should* look like.
@@ -394,6 +397,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
394397
i.kind.is_bytestr().then(|| self.sess().source_map().start_point(i.span))
395398
}),
396399
},
400+
attr_style: self.attr_style,
397401
})
398402
}
399403

@@ -404,6 +408,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
404408
template: self.template.clone(),
405409
attribute: self.attr_path.clone(),
406410
reason: AttributeParseErrorReason::ExpectedIntegerLiteral,
411+
attr_style: self.attr_style,
407412
})
408413
}
409414

@@ -414,6 +419,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
414419
template: self.template.clone(),
415420
attribute: self.attr_path.clone(),
416421
reason: AttributeParseErrorReason::ExpectedList,
422+
attr_style: self.attr_style,
417423
})
418424
}
419425

@@ -424,6 +430,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
424430
template: self.template.clone(),
425431
attribute: self.attr_path.clone(),
426432
reason: AttributeParseErrorReason::ExpectedNoArgs,
433+
attr_style: self.attr_style,
427434
})
428435
}
429436

@@ -435,6 +442,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
435442
template: self.template.clone(),
436443
attribute: self.attr_path.clone(),
437444
reason: AttributeParseErrorReason::ExpectedIdentifier,
445+
attr_style: self.attr_style,
438446
})
439447
}
440448

@@ -447,6 +455,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
447455
template: self.template.clone(),
448456
attribute: self.attr_path.clone(),
449457
reason: AttributeParseErrorReason::ExpectedNameValue(name),
458+
attr_style: self.attr_style,
450459
})
451460
}
452461

@@ -458,6 +467,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
458467
template: self.template.clone(),
459468
attribute: self.attr_path.clone(),
460469
reason: AttributeParseErrorReason::DuplicateKey(key),
470+
attr_style: self.attr_style,
461471
})
462472
}
463473

@@ -470,6 +480,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
470480
template: self.template.clone(),
471481
attribute: self.attr_path.clone(),
472482
reason: AttributeParseErrorReason::UnexpectedLiteral,
483+
attr_style: self.attr_style,
473484
})
474485
}
475486

@@ -480,6 +491,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
480491
template: self.template.clone(),
481492
attribute: self.attr_path.clone(),
482493
reason: AttributeParseErrorReason::ExpectedSingleArgument,
494+
attr_style: self.attr_style,
483495
})
484496
}
485497

@@ -490,6 +502,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
490502
template: self.template.clone(),
491503
attribute: self.attr_path.clone(),
492504
reason: AttributeParseErrorReason::ExpectedAtLeastOneArgument,
505+
attr_style: self.attr_style,
493506
})
494507
}
495508

@@ -508,6 +521,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
508521
strings: false,
509522
list: false,
510523
},
524+
attr_style: self.attr_style,
511525
})
512526
}
513527

@@ -526,6 +540,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
526540
strings: false,
527541
list: true,
528542
},
543+
attr_style: self.attr_style,
529544
})
530545
}
531546

@@ -544,6 +559,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
544559
strings: true,
545560
list: false,
546561
},
562+
attr_style: self.attr_style,
547563
})
548564
}
549565

@@ -802,6 +818,7 @@ impl<'sess> AttributeParser<'sess, Early> {
802818
},
803819
},
804820
attr_span: attr.span,
821+
attr_style: attr.style,
805822
template,
806823
attr_path: path.get_attribute_path(),
807824
};
@@ -912,6 +929,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
912929
emit_lint: &mut emit_lint,
913930
},
914931
attr_span: lower_span(attr.span),
932+
attr_style: attr.style,
915933
template: &accept.template,
916934
attr_path: path.get_attribute_path(),
917935
};
@@ -1060,6 +1078,9 @@ pub(crate) fn allowed_targets_applied(
10601078
if !features.stmt_expr_attributes() {
10611079
allowed_targets.retain(|t| !matches!(t, Target::Expression | Target::Statement));
10621080
}
1081+
if !features.extern_types() {
1082+
allowed_targets.retain(|t| !matches!(t, Target::ForeignTy));
1083+
}
10631084
}
10641085

10651086
// We define groups of "similar" targets.

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::num::IntErrorKind;
22

3-
use rustc_ast as ast;
3+
use rustc_ast::{self as ast, AttrStyle};
44
use rustc_errors::codes::*;
55
use rustc_errors::{
66
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level,
@@ -579,6 +579,7 @@ pub(crate) enum AttributeParseErrorReason {
579579
pub(crate) struct AttributeParseError {
580580
pub(crate) span: Span,
581581
pub(crate) attr_span: Span,
582+
pub(crate) attr_style: AttrStyle,
582583
pub(crate) template: AttributeTemplate,
583584
pub(crate) attribute: AttrPath,
584585
pub(crate) reason: AttributeParseErrorReason,
@@ -717,7 +718,8 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
717718
if let Some(link) = self.template.docs {
718719
diag.note(format!("for more information, visit <{link}>"));
719720
}
720-
let suggestions = self.template.suggestions(false, &name);
721+
let suggestions = self.template.suggestions(self.attr_style, &name);
722+
721723
diag.span_suggestions(
722724
self.attr_span,
723725
if suggestions.len() == 1 {

0 commit comments

Comments
 (0)