Skip to content

Commit dd46057

Browse files
authored
Rollup merge of #154653 - mejrs:append_const_msg, r=JonathanBrouwer
Remove rustc_on_unimplemented's append_const_msg This does nothing because it is unreachable within the compiler. It also doesn't seem that useful since all the traits it was on are `const` now. Will make pr to rustc-dev-guide docs later.
2 parents ce74af2 + b6bdfed commit dd46057

9 files changed

Lines changed: 42 additions & 100 deletions

File tree

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::ops::Range;
33
use rustc_errors::E0232;
44
use rustc_hir::AttrPath;
55
use rustc_hir::attrs::diagnostic::{
6-
AppendConstMessage, Directive, FilterFormatString, Flag, FormatArg, FormatString, LitOrArg,
7-
Name, NameValue, OnUnimplementedCondition, Piece, Predicate,
6+
Directive, FilterFormatString, Flag, FormatArg, FormatString, LitOrArg, Name, NameValue,
7+
OnUnimplementedCondition, Piece, Predicate,
88
};
99
use rustc_hir::lints::{AttributeLintKind, FormatWarning};
1010
use rustc_macros::Diagnostic;
@@ -92,7 +92,6 @@ fn parse_directive_items<'p, S: Stage>(
9292
let mut notes = ThinVec::new();
9393
let mut parent_label = None;
9494
let mut subcommands = ThinVec::new();
95-
let mut append_const_msg = None;
9695

9796
for item in items {
9897
let span = item.span();
@@ -131,7 +130,6 @@ fn parse_directive_items<'p, S: Stage>(
131130
let Some(ret) = (||{
132131
Some($($code)*)
133132
})() else {
134-
135133
malformed!()
136134
};
137135
ret
@@ -159,8 +157,13 @@ fn parse_directive_items<'p, S: Stage>(
159157
let item: &MetaItemParser = or_malformed!(item.meta_item()?);
160158
let name = or_malformed!(item.ident()?).name;
161159

162-
// Some things like `message = "message"` must have a value.
163-
// But with things like `append_const_msg` that is optional.
160+
// Currently, as of April 2026, all arguments of all diagnostic attrs
161+
// must have a value, like `message = "message"`. Thus in a well-formed
162+
// diagnostic attribute this is never `None`.
163+
//
164+
// But we don't assert its presence yet because we don't want to mention it
165+
// if someone does something like `#[diagnostic::on_unimplemented(doesnt_exist)]`.
166+
// That happens in the big `match` below.
164167
let value: Option<Ident> = match item.args().name_value() {
165168
Some(nv) => Some(or_malformed!(nv.value_as_ident()?)),
166169
None => None,
@@ -223,14 +226,6 @@ fn parse_directive_items<'p, S: Stage>(
223226
let value = or_malformed!(value?);
224227
notes.push(parse_format(value))
225228
}
226-
227-
(Mode::RustcOnUnimplemented, sym::append_const_msg) => {
228-
append_const_msg = if let Some(msg) = value {
229-
Some(AppendConstMessage::Custom(msg.name, item.span()))
230-
} else {
231-
Some(AppendConstMessage::Default)
232-
}
233-
}
234229
(Mode::RustcOnUnimplemented, sym::parent_label) => {
235230
let value = or_malformed!(value?);
236231
if parent_label.is_none() {
@@ -290,7 +285,6 @@ fn parse_directive_items<'p, S: Stage>(
290285
label,
291286
notes,
292287
parent_label,
293-
append_const_msg,
294288
})
295289
}
296290

compiler/rustc_hir/src/attrs/diagnostic.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub struct Directive {
1919
pub label: Option<(Span, FormatString)>,
2020
pub notes: ThinVec<FormatString>,
2121
pub parent_label: Option<FormatString>,
22-
pub append_const_msg: Option<AppendConstMessage>,
2322
}
2423

2524
impl Directive {
@@ -63,7 +62,6 @@ impl Directive {
6362
let mut label = None;
6463
let mut notes = Vec::new();
6564
let mut parent_label = None;
66-
let mut append_const_msg = None;
6765
info!(
6866
"evaluate_directive({:?}, trait_ref={:?}, options={:?}, args ={:?})",
6967
self, trait_name, condition_options, args
@@ -91,16 +89,13 @@ impl Directive {
9189
if let Some(ref parent_label_) = command.parent_label {
9290
parent_label = Some(parent_label_.clone());
9391
}
94-
95-
append_const_msg = command.append_const_msg;
9692
}
9793

9894
OnUnimplementedNote {
9995
label: label.map(|l| l.1.format(args)),
10096
message: message.map(|m| m.1.format(args)),
10197
notes: notes.into_iter().map(|n| n.format(args)).collect(),
10298
parent_label: parent_label.map(|e_s| e_s.format(args)),
103-
append_const_msg,
10499
}
105100
}
106101
}
@@ -111,17 +106,6 @@ pub struct OnUnimplementedNote {
111106
pub label: Option<String>,
112107
pub notes: Vec<String>,
113108
pub parent_label: Option<String>,
114-
// If none, should fall back to a generic message
115-
pub append_const_msg: Option<AppendConstMessage>,
116-
}
117-
118-
/// Append a message for `[const] Trait` errors.
119-
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
120-
#[derive(HashStable_Generic, Encodable, Decodable, PrintAttribute)]
121-
pub enum AppendConstMessage {
122-
#[default]
123-
Default,
124-
Custom(Symbol, Span),
125109
}
126110

127111
/// Like [std::fmt::Arguments] this is a string that has been parsed into "pieces",

compiler/rustc_span/src/symbol.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ symbols! {
399399
anon_assoc,
400400
anonymous_lifetime_in_impl_trait,
401401
any,
402-
append_const_msg,
403402
apx_target_feature,
404403
arbitrary_enum_discriminant,
405404
arbitrary_self_types,

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_errors::{
1414
Applicability, Diag, ErrorGuaranteed, Level, MultiSpan, StashKey, StringPart, Suggestions, msg,
1515
pluralize, struct_span_code_err,
1616
};
17-
use rustc_hir::attrs::diagnostic::{AppendConstMessage, OnUnimplementedNote};
17+
use rustc_hir::attrs::diagnostic::OnUnimplementedNote;
1818
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
1919
use rustc_hir::intravisit::Visitor;
2020
use rustc_hir::{self as hir, LangItem, Node, find_attr};
@@ -193,10 +193,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
193193
label,
194194
notes,
195195
parent_label,
196-
append_const_msg,
197196
} = self.on_unimplemented_note(main_trait_predicate, main_obligation, &mut long_ty_file);
198197

199198
let have_alt_message = message.is_some() || label.is_some();
199+
200+
let message = message.unwrap_or_else(|| self.get_standard_error_message(
201+
main_trait_predicate,
202+
None,
203+
post_message,
204+
&mut long_ty_file,
205+
));
200206
let is_try_conversion = self.is_try_conversion(span, main_trait_predicate.def_id());
201207
let is_question_mark = matches!(
202208
root_obligation.cause.code().peel_derives(),
@@ -210,16 +216,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
210216
let question_mark_message = "the question mark operation (`?`) implicitly \
211217
performs a conversion on the error value \
212218
using the `From` trait";
213-
let (message, notes, append_const_msg) = if is_try_conversion {
219+
let (message, notes) = if is_try_conversion {
214220
let ty = self.tcx.short_string(
215221
main_trait_predicate.skip_binder().self_ty(),
216222
&mut long_ty_file,
217223
);
218224
// We have a `-> Result<_, E1>` and `gives_E2()?`.
219225
(
220-
Some(format!("`?` couldn't convert the error to `{ty}`")),
226+
format!("`?` couldn't convert the error to `{ty}`"),
221227
vec![question_mark_message.to_owned()],
222-
Some(AppendConstMessage::Default),
223228
)
224229
} else if is_question_mark {
225230
let main_trait_predicate =
@@ -228,26 +233,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
228233
// trait object: `-> Result<_, Box<dyn Error>` and `gives_E()?` when
229234
// `E: Error` isn't met.
230235
(
231-
Some(format!(
236+
format!(
232237
"`?` couldn't convert the error: `{main_trait_predicate}` is \
233238
not satisfied",
234-
)),
239+
),
235240
vec![question_mark_message.to_owned()],
236-
Some(AppendConstMessage::Default),
237241
)
238242
} else {
239-
(message, notes, append_const_msg)
243+
(message, notes)
240244
};
241245

242-
let default_err_msg = || self.get_standard_error_message(
243-
main_trait_predicate,
244-
message,
245-
None,
246-
append_const_msg,
247-
post_message,
248-
&mut long_ty_file,
249-
);
250-
251246
let (err_msg, safe_transmute_explanation) = if self.tcx.is_lang_item(
252247
main_trait_predicate.def_id(),
253248
LangItem::TransmuteTrait,
@@ -271,15 +266,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
271266
);
272267
}
273268
GetSafeTransmuteErrorAndReason::Default => {
274-
(default_err_msg(), None)
269+
(message, None)
275270
}
276271
GetSafeTransmuteErrorAndReason::Error {
277272
err_msg,
278273
safe_transmute_explanation,
279274
} => (err_msg, safe_transmute_explanation),
280275
}
281276
} else {
282-
(default_err_msg(), None)
277+
(message, None)
283278
};
284279

285280
let mut err = struct_span_code_err!(self.dcx(), span, E0277, "{}", err_msg);
@@ -393,7 +388,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
393388
if let Some(s) = label {
394389
// If it has a custom `#[rustc_on_unimplemented]`
395390
// error message, let's display it as the label!
396-
err.span_label(span, s.as_str().to_owned());
391+
err.span_label(span, s);
397392
if !matches!(leaf_trait_predicate.skip_binder().self_ty().kind(), ty::Param(_))
398393
// When the self type is a type param We don't need to "the trait
399394
// `std::marker::Sized` is not implemented for `T`" as we will point
@@ -862,9 +857,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
862857

863858
let err_msg = self.get_standard_error_message(
864859
trait_ref,
865-
None,
866860
Some(predicate.constness()),
867-
None,
868861
String::new(),
869862
&mut file,
870863
);
@@ -924,7 +917,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
924917
label,
925918
notes,
926919
parent_label,
927-
append_const_msg: _,
928920
} = note;
929921

930922
if let Some(message) = message {
@@ -2841,40 +2833,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
28412833
fn get_standard_error_message(
28422834
&self,
28432835
trait_predicate: ty::PolyTraitPredicate<'tcx>,
2844-
message: Option<String>,
28452836
predicate_constness: Option<ty::BoundConstness>,
2846-
append_const_msg: Option<AppendConstMessage>,
28472837
post_message: String,
28482838
long_ty_path: &mut Option<PathBuf>,
28492839
) -> String {
2850-
message
2851-
.and_then(|cannot_do_this| {
2852-
match (predicate_constness, append_const_msg) {
2853-
// do nothing if predicate is not const
2854-
(None, _) => Some(cannot_do_this),
2855-
// suggested using default post message
2856-
(
2857-
Some(ty::BoundConstness::Const | ty::BoundConstness::Maybe),
2858-
Some(AppendConstMessage::Default),
2859-
) => Some(format!("{cannot_do_this} in const contexts")),
2860-
// overridden post message
2861-
(
2862-
Some(ty::BoundConstness::Const | ty::BoundConstness::Maybe),
2863-
Some(AppendConstMessage::Custom(custom_msg, _)),
2864-
) => Some(format!("{cannot_do_this}{custom_msg}")),
2865-
// fallback to generic message
2866-
(Some(ty::BoundConstness::Const | ty::BoundConstness::Maybe), None) => None,
2867-
}
2868-
})
2869-
.unwrap_or_else(|| {
2870-
format!(
2871-
"the trait bound `{}` is not satisfied{post_message}",
2872-
self.tcx.short_string(
2873-
trait_predicate.print_with_bound_constness(predicate_constness),
2874-
long_ty_path,
2875-
),
2876-
)
2877-
})
2840+
format!(
2841+
"the trait bound `{}` is not satisfied{post_message}",
2842+
self.tcx.short_string(
2843+
trait_predicate.print_with_bound_constness(predicate_constness),
2844+
long_ty_path,
2845+
),
2846+
)
28782847
}
28792848

28802849
fn select_transmute_obligation_for_reporting(

library/core/src/cmp.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,9 @@ use crate::ops::ControlFlow;
241241
#[stable(feature = "rust1", since = "1.0.0")]
242242
#[doc(alias = "==")]
243243
#[doc(alias = "!=")]
244-
#[rustc_on_unimplemented(
244+
#[diagnostic::on_unimplemented(
245245
message = "can't compare `{Self}` with `{Rhs}`",
246-
label = "no implementation for `{Self} == {Rhs}`",
247-
append_const_msg
246+
label = "no implementation for `{Self} == {Rhs}`"
248247
)]
249248
#[rustc_diagnostic_item = "PartialEq"]
250249
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
@@ -1356,10 +1355,9 @@ pub macro Ord($item:item) {
13561355
#[doc(alias = "<")]
13571356
#[doc(alias = "<=")]
13581357
#[doc(alias = ">=")]
1359-
#[rustc_on_unimplemented(
1358+
#[diagnostic::on_unimplemented(
13601359
message = "can't compare `{Self}` with `{Rhs}`",
1361-
label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
1362-
append_const_msg
1360+
label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`"
13631361
)]
13641362
#[rustc_diagnostic_item = "PartialOrd"]
13651363
#[allow(multiple_supertrait_upcastable)] // FIXME(sized_hierarchy): remove this

library/core/src/iter/range.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ unsafe_impl_trusted_step![AsciiChar char i8 i16 i32 i64 i128 isize u8 u16 u32 u6
2121
/// The *successor* operation moves towards values that compare greater.
2222
/// The *predecessor* operation moves towards values that compare lesser.
2323
#[rustc_diagnostic_item = "range_step"]
24-
#[rustc_on_unimplemented(
24+
#[diagnostic::on_unimplemented(
2525
message = "`std::ops::Range<{Self}>` is not an iterator",
2626
label = "`Range<{Self}>` is not an iterator",
2727
note = "`Range` only implements `Iterator` for select types in the standard library, \

library/core/src/marker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ marker_impls! {
10541054
#[unstable(feature = "const_destruct", issue = "133214")]
10551055
#[rustc_const_unstable(feature = "const_destruct", issue = "133214")]
10561056
#[lang = "destruct"]
1057-
#[rustc_on_unimplemented(message = "can't drop `{Self}`", append_const_msg)]
1057+
#[diagnostic::on_unimplemented(message = "can't drop `{Self}`")]
10581058
#[rustc_deny_explicit_impl]
10591059
#[rustc_dyn_incompatible_trait]
10601060
pub const trait Destruct: PointeeSized {}

library/core/src/ops/arith.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@
7070
on(all(Self = "{integer}", Rhs = "{float}"), message = "cannot add a float to an integer",),
7171
on(all(Self = "{float}", Rhs = "{integer}"), message = "cannot add an integer to a float",),
7272
message = "cannot add `{Rhs}` to `{Self}`",
73-
label = "no implementation for `{Self} + {Rhs}`",
74-
append_const_msg
73+
label = "no implementation for `{Self} + {Rhs}`"
7574
)]
7675
#[doc(alias = "+")]
7776
pub const trait Add<Rhs = Self> {
@@ -181,10 +180,9 @@ add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128
181180
#[lang = "sub"]
182181
#[stable(feature = "rust1", since = "1.0.0")]
183182
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
184-
#[rustc_on_unimplemented(
183+
#[diagnostic::on_unimplemented(
185184
message = "cannot subtract `{Rhs}` from `{Self}`",
186-
label = "no implementation for `{Self} - {Rhs}`",
187-
append_const_msg
185+
label = "no implementation for `{Self} - {Rhs}`"
188186
)]
189187
#[doc(alias = "-")]
190188
pub const trait Sub<Rhs = Self> {

tests/auxiliary/minicore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub trait MetaSized: PointeeSized {}
6363
pub trait Sized: MetaSized {}
6464

6565
#[lang = "destruct"]
66-
#[rustc_on_unimplemented(message = "can't drop `{Self}`", append_const_msg)]
66+
#[diagnostic::on_unimplemented(message = "can't drop `{Self}`")]
6767
pub trait Destruct: PointeeSized {}
6868

6969
#[lang = "legacy_receiver"]

0 commit comments

Comments
 (0)