Clean up OnUnimplementedFormatString::verify

This commit is contained in:
clubby789 2023-01-11 22:54:46 +00:00
parent ef4046e4f3
commit b78a571ce1

View File

@ -37,6 +37,21 @@ fn on_unimplemented_note(
) -> OnUnimplementedNote; ) -> OnUnimplementedNote;
} }
/// The symbols which are always allowed in a format string
static ALLOWED_FORMAT_SYMBOLS: &[Symbol] = &[
kw::SelfUpper,
sym::ItemContext,
sym::from_method,
sym::from_desugaring,
sym::direct,
sym::cause,
sym::integral,
sym::integer_,
sym::float,
sym::_Self,
sym::crate_local,
];
impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
fn impl_similar_to( fn impl_similar_to(
&self, &self,
@ -543,38 +558,26 @@ fn verify(
Piece::NextArgument(a) => match a.position { Piece::NextArgument(a) => match a.position {
Position::ArgumentNamed(s) => { Position::ArgumentNamed(s) => {
match Symbol::intern(s) { match Symbol::intern(s) {
// `{Self}` is allowed
kw::SelfUpper => (),
// `{ThisTraitsName}` is allowed // `{ThisTraitsName}` is allowed
s if s == trait_name => (), s if s == trait_name => (),
// `{from_method}` is allowed s if ALLOWED_FORMAT_SYMBOLS.contains(&s) => (),
sym::from_method => (),
// `{from_desugaring}` is allowed
sym::from_desugaring => (),
// `{ItemContext}` is allowed
sym::ItemContext => (),
// `{integral}` and `{integer}` and `{float}` are allowed
sym::integral | sym::integer_ | sym::float => (),
// So is `{A}` if A is a type parameter // So is `{A}` if A is a type parameter
s => match generics.params.iter().find(|param| param.name == s) { s if generics.params.iter().any(|param| param.name == s) => (),
Some(_) => (), s => {
None => { result = Err(struct_span_err!(
let reported = struct_span_err!( tcx.sess,
tcx.sess, span,
span, E0230,
E0230, "there is no parameter `{}` on {}",
"there is no parameter `{}` on {}", s,
s, if trait_def_id == item_def_id {
if trait_def_id == item_def_id { format!("trait `{}`", trait_name)
format!("trait `{}`", trait_name) } else {
} else { "impl".to_string()
"impl".to_string() }
} )
) .emit());
.emit(); }
result = Err(reported);
}
},
} }
} }
// `{:1}` and `{}` are not to be used // `{:1}` and `{}` are not to be used