Skip to content

Commit 71dc761

Browse files
committed
Auto merge of rust-lang#152382 - jhpratt:rollup-DIUVWuF, r=jhpratt
Rollup of 3 pull requests Successful merges: - rust-lang#152357 (std: Don't panic when removing a nonexistent UEFI var) - rust-lang#152180 (Port `rustc_reservation_impl` to the new attribute parser) - rust-lang#152276 (Add message format checking)
2 parents c6936c3 + 9d31498 commit 71dc761

File tree

18 files changed

+219
-140
lines changed

18 files changed

+219
-140
lines changed

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,3 +873,29 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcStrictCoherenceParser {
873873
]);
874874
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcStrictCoherence;
875875
}
876+
877+
pub(crate) struct RustcReservationImplParser;
878+
879+
impl<S: Stage> SingleAttributeParser<S> for RustcReservationImplParser {
880+
const PATH: &[Symbol] = &[sym::rustc_reservation_impl];
881+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
882+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
883+
const ALLOWED_TARGETS: AllowedTargets =
884+
AllowedTargets::AllowList(&[Allow(Target::Impl { of_trait: true })]);
885+
886+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "reservation message");
887+
888+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
889+
let Some(nv) = args.name_value() else {
890+
cx.expected_name_value(args.span().unwrap_or(cx.attr_span), None);
891+
return None;
892+
};
893+
894+
let Some(value_str) = nv.value_as_str() else {
895+
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
896+
return None;
897+
};
898+
899+
Some(AttributeKind::RustcReservationImpl(cx.attr_span, value_str))
900+
}
901+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ attribute_parsers!(
209209
Single<RustcLintOptDenyFieldAccessParser>,
210210
Single<RustcMustImplementOneOfParser>,
211211
Single<RustcObjectLifetimeDefaultParser>,
212+
Single<RustcReservationImplParser>,
212213
Single<RustcScalableVectorParser>,
213214
Single<RustcSimdMonomorphizeLaneLimitParser>,
214215
Single<RustcSymbolName>,

compiler/rustc_const_eval/src/errors.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ pub(crate) struct UnstableInStableExposed {
106106
pub is_function_call2: bool,
107107
#[suggestion(
108108
"if the {$is_function_call2 ->
109-
[true] caller
110-
*[false] function
111-
} is not (yet) meant to be exposed to stable const contexts, add `#[rustc_const_unstable]`",
109+
[true] caller
110+
*[false] function
111+
} is not (yet) meant to be exposed to stable const contexts, add `#[rustc_const_unstable]`",
112112
code = "#[rustc_const_unstable(feature = \"...\", issue = \"...\")]\n",
113113
applicability = "has-placeholders"
114114
)]
@@ -300,11 +300,11 @@ pub(crate) struct UnallowedHeapAllocations {
300300
#[primary_span]
301301
#[label(
302302
r#"allocation not allowed in {$kind ->
303-
[const] constant
304-
[static] static
305-
[const_fn] constant function
306-
*[other] {""}
307-
}s"#
303+
[const] constant
304+
[static] static
305+
[const_fn] constant function
306+
*[other] {""}
307+
}s"#
308308
)]
309309
pub span: Span,
310310
pub kind: ConstContext,
@@ -539,20 +539,20 @@ pub enum NonConstClosureNote {
539539
},
540540
#[note(
541541
r#"function pointers need an RFC before allowed to be called in {$kind ->
542-
[const] constant
543-
[static] static
544-
[const_fn] constant function
545-
*[other] {""}
546-
}s"#
542+
[const] constant
543+
[static] static
544+
[const_fn] constant function
545+
*[other] {""}
546+
}s"#
547547
)]
548548
FnPtr,
549549
#[note(
550550
r#"closures need an RFC before allowed to be called in {$kind ->
551-
[const] constant
552-
[static] static
553-
[const_fn] constant function
554-
*[other] {""}
555-
}s"#
551+
[const] constant
552+
[static] static
553+
[const_fn] constant function
554+
*[other] {""}
555+
}s"#
556556
)]
557557
Closure,
558558
}
@@ -608,11 +608,11 @@ pub struct LiveDrop<'tcx> {
608608
#[primary_span]
609609
#[label(
610610
r#"the destructor for this type cannot be evaluated in {$kind ->
611-
[const] constant
612-
[static] static
613-
[const_fn] constant function
614-
*[other] {""}
615-
}s"#
611+
[const] constant
612+
[static] static
613+
[const_fn] constant function
614+
*[other] {""}
615+
}s"#
616616
)]
617617
pub span: Span,
618618
pub kind: ConstContext,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,9 @@ pub enum AttributeKind {
12191219
/// Represents `#[rustc_regions]`
12201220
RustcRegions,
12211221

1222+
/// Represents `#[rustc_reservation_impl]`
1223+
RustcReservationImpl(Span, Symbol),
1224+
12221225
/// Represents `#[rustc_scalable_vector(N)]`
12231226
RustcScalableVector {
12241227
/// The base multiple of lanes that are in a scalable vector, if provided. `element_count`

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl AttributeKind {
151151
RustcPubTransparent(..) => Yes,
152152
RustcReallocator => No,
153153
RustcRegions => No,
154+
RustcReservationImpl(..) => Yes,
154155
RustcScalableVector { .. } => Yes,
155156
RustcShouldNotBeCalledOnConstItems(..) => Yes,
156157
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,8 @@ fn impl_trait_header(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplTraitHeader
12751275
.of_trait
12761276
.unwrap_or_else(|| panic!("expected impl trait, found inherent impl on {def_id:?}"));
12771277
let selfty = tcx.type_of(def_id).instantiate_identity();
1278-
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
1278+
let is_rustc_reservation =
1279+
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcReservationImpl(..));
12791280

12801281
check_impl_constness(tcx, impl_.constness, &of_trait.trait_ref);
12811282

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ impl DiagnosticDeriveVariantBuilder {
203203
)
204204
.emit();
205205
}
206-
self.message =
207-
Some(Message { message_span: message.span(), value: message.value() });
206+
self.message = Some(Message {
207+
attr_span: attr.span(),
208+
message_span: message.span(),
209+
value: message.value(),
210+
});
208211
}
209212

210213
// Parse arguments

compiler/rustc_macros/src/diagnostics/message.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::diagnostics::error::span_err;
99

1010
#[derive(Clone)]
1111
pub(crate) struct Message {
12+
pub attr_span: Span,
1213
pub message_span: Span,
1314
pub value: String,
1415
}
@@ -19,12 +20,18 @@ impl Message {
1920
/// For subdiagnostics, we cannot check this.
2021
pub(crate) fn diag_message(&self, variant: Option<&VariantInfo<'_>>) -> TokenStream {
2122
let message = &self.value;
22-
verify_fluent_message(self.message_span, &message, variant);
23+
self.verify(variant);
2324
quote! { rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed(#message)) }
2425
}
26+
27+
fn verify(&self, variant: Option<&VariantInfo<'_>>) {
28+
verify_variables_used(self.message_span, &self.value, variant);
29+
verify_message_style(self.message_span, &self.value);
30+
verify_message_formatting(self.attr_span, self.message_span, &self.value);
31+
}
2532
}
2633

27-
fn verify_fluent_message(msg_span: Span, message_str: &str, variant: Option<&VariantInfo<'_>>) {
34+
fn verify_variables_used(msg_span: Span, message_str: &str, variant: Option<&VariantInfo<'_>>) {
2835
// Parse the fluent message
2936
const GENERATED_MSG_ID: &str = "generated_msg";
3037
let resource =
@@ -53,8 +60,6 @@ fn verify_fluent_message(msg_span: Span, message_str: &str, variant: Option<&Var
5360
}
5461
}
5562
}
56-
57-
verify_message_style(msg_span, message_str);
5863
}
5964

6065
fn variable_references<'a>(msg: &fluent_syntax::ast::Message<&'a str>) -> Vec<&'a str> {
@@ -120,3 +125,29 @@ fn verify_message_style(msg_span: Span, message: &str) {
120125
return;
121126
}
122127
}
128+
129+
/// Verifies that the message is properly indented into the code
130+
fn verify_message_formatting(attr_span: Span, msg_span: Span, message: &str) {
131+
// Find the indent at the start of the message (`column()` is one-indexed)
132+
let start = attr_span.unwrap().column() - 1;
133+
134+
for line in message.lines().skip(1) {
135+
if line.is_empty() {
136+
continue;
137+
}
138+
let indent = line.chars().take_while(|c| *c == ' ').count();
139+
if indent < start {
140+
span_err(
141+
msg_span.unwrap(),
142+
format!("message is not properly indented. {indent} < {start}"),
143+
)
144+
.emit();
145+
return;
146+
}
147+
if indent % 4 != 0 {
148+
span_err(msg_span.unwrap(), "message is not indented with a multiple of 4 spaces")
149+
.emit();
150+
return;
151+
}
152+
}
153+
}

compiler/rustc_macros/src/diagnostics/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl SubdiagnosticVariant {
708708
}
709709
if !input.is_empty() { input.parse::<Token![,]>()?; }
710710
if is_first {
711-
message = Some(Message { message_span: inline_message.span(), value: inline_message.value() });
711+
message = Some(Message { attr_span: attr.span(), message_span: inline_message.span(), value: inline_message.value() });
712712
is_first = false;
713713
} else {
714714
span_err(inline_message.span().unwrap(), "a diagnostic message must be the first argument to the attribute").emit();

compiler/rustc_mir_build/src/errors.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ pub(crate) struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
198198
pub(crate) missing_target_features: DiagArgValue,
199199
pub(crate) missing_target_features_count: usize,
200200
#[note("the {$build_target_features} target {$build_target_features_count ->
201-
[1] feature
202-
*[count] features
201+
[1] feature
202+
*[count] features
203203
} being enabled in the build configuration does not remove the requirement to list {$build_target_features_count ->
204-
[1] it
205-
*[count] them
206-
} in `#[target_feature]`")]
204+
[1] it
205+
*[count] them
206+
} in `#[target_feature]`")]
207207
pub(crate) note: bool,
208208
pub(crate) build_target_features: DiagArgValue,
209209
pub(crate) build_target_features_count: usize,
@@ -532,12 +532,12 @@ pub(crate) struct CallToFunctionWithRequiresUnsafe {
532532
pub(crate) missing_target_features: DiagArgValue,
533533
pub(crate) missing_target_features_count: usize,
534534
#[note("the {$build_target_features} target {$build_target_features_count ->
535-
[1] feature
536-
*[count] features
537-
} being enabled in the build configuration does not remove the requirement to list {$build_target_features_count ->
538-
[1] it
539-
*[count] them
540-
} in `#[target_feature]`")]
535+
[1] feature
536+
*[count] features
537+
} being enabled in the build configuration does not remove the requirement to list {$build_target_features_count ->
538+
[1] it
539+
*[count] them
540+
} in `#[target_feature]`")]
541541
pub(crate) note: bool,
542542
pub(crate) build_target_features: DiagArgValue,
543543
pub(crate) build_target_features_count: usize,
@@ -1264,9 +1264,9 @@ pub(crate) struct InterpretedAsConstSugg {
12641264
pub(crate) enum SuggestLet {
12651265
#[multipart_suggestion(
12661266
"you might want to use `if let` to ignore the {$count ->
1267-
[one] variant that isn't
1268-
*[other] variants that aren't
1269-
} matched",
1267+
[one] variant that isn't
1268+
*[other] variants that aren't
1269+
} matched",
12701270
applicability = "has-placeholders"
12711271
)]
12721272
If {
@@ -1278,9 +1278,9 @@ pub(crate) enum SuggestLet {
12781278
},
12791279
#[suggestion(
12801280
"you might want to use `let...else` to handle the {$count ->
1281-
[one] variant that isn't
1282-
*[other] variants that aren't
1283-
} matched",
1281+
[one] variant that isn't
1282+
*[other] variants that aren't
1283+
} matched",
12841284
code = " else {{ todo!() }}",
12851285
applicability = "has-placeholders"
12861286
)]

0 commit comments

Comments
 (0)