@@ -37,6 +37,21 @@ pub trait TypeErrCtxtExt<'tcx> {
3737 ) -> OnUnimplementedNote;
3838}
3939
40+ /// The symbols which are always allowed in a format string
41+ static ALLOWED_FORMAT_SYMBOLS: &[Symbol] = &[
42+ kw::SelfUpper,
43+ sym::ItemContext,
44+ sym::from_method,
45+ sym::from_desugaring,
46+ sym::direct,
47+ sym::cause,
48+ sym::integral,
49+ sym::integer_,
50+ sym::float,
51+ sym::_Self,
52+ sym::crate_local,
53+ ];
54+
4055impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
4156 fn impl_similar_to(
4257 &self,
@@ -543,38 +558,26 @@ impl<'tcx> OnUnimplementedFormatString {
543558 Piece::NextArgument(a) => match a.position {
544559 Position::ArgumentNamed(s) => {
545560 match Symbol::intern(s) {
546- // `{Self}` is allowed
547- kw::SelfUpper => (),
548561 // `{ThisTraitsName}` is allowed
549562 s if s == trait_name => (),
550- // `{from_method}` is allowed
551- sym::from_method => (),
552- // `{from_desugaring}` is allowed
553- sym::from_desugaring => (),
554- // `{ItemContext}` is allowed
555- sym::ItemContext => (),
556- // `{integral}` and `{integer}` and `{float}` are allowed
557- sym::integral | sym::integer_ | sym::float => (),
563+ s if ALLOWED_FORMAT_SYMBOLS.contains(&s) => (),
558564 // So is `{A}` if A is a type parameter
559- s => match generics.params.iter().find(|param| param.name == s) {
560- Some(_) => (),
561- None => {
562- let reported = struct_span_err!(
563- tcx.sess,
564- span,
565- E0230,
566- "there is no parameter `{}` on {}",
567- s,
568- if trait_def_id == item_def_id {
569- format!("trait `{}`", trait_name)
570- } else {
571- "impl".to_string()
572- }
573- )
574- .emit();
575- result = Err(reported);
576- }
577- },
565+ s if generics.params.iter().any(|param| param.name == s) => (),
566+ s => {
567+ result = Err(struct_span_err!(
568+ tcx.sess,
569+ span,
570+ E0230,
571+ "there is no parameter `{}` on {}",
572+ s,
573+ if trait_def_id == item_def_id {
574+ format!("trait `{}`", trait_name)
575+ } else {
576+ "impl".to_string()
577+ }
578+ )
579+ .emit());
580+ }
578581 }
579582 }
580583 // `{:1}` and `{}` are not to be used
0 commit comments