Skip to content

Commit cd7c818

Browse files
committed
Add more targetting filters for arrays to rustc_on_unimplemented
1 parent 0e07c42 commit cd7c818

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
349349
fn on_unimplemented_note(
350350
&self,
351351
trait_ref: ty::PolyTraitRef<'tcx>,
352-
obligation: &PredicateObligation<'tcx>) ->
353-
OnUnimplementedNote
354-
{
355-
let def_id = self.impl_similar_to(trait_ref, obligation)
352+
obligation: &PredicateObligation<'tcx>,
353+
) -> OnUnimplementedNote {
354+
let def_id = self.impl_similar_to(trait_ref, obligation)
356355
.unwrap_or(trait_ref.def_id());
357356
let trait_ref = *trait_ref.skip_binder();
358357

@@ -410,6 +409,37 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
410409
flags.push(("crate_local".to_owned(), None));
411410
}
412411

412+
// Allow targetting all integers using `{integral}`, even if the exact type was resolved
413+
if self_ty.is_integral() {
414+
flags.push(("_Self".to_owned(), Some("{integral}".to_owned())));
415+
}
416+
417+
if let ty::Array(aty, len) = self_ty.sty {
418+
flags.push(("_Self".to_owned(), Some("[]".to_owned())));
419+
flags.push(("_Self".to_owned(), Some(format!("[{}]", aty))));
420+
if let Some(def) = aty.ty_adt_def() {
421+
// We also want to be able to select the array's type's original
422+
// signature with no type arguments resolved
423+
flags.push((
424+
"_Self".to_owned(),
425+
Some(format!("[{}]", self.tcx.type_of(def.did).to_string())),
426+
));
427+
if let Some(len) = len.val.try_to_scalar().and_then(|scalar| {
428+
scalar.to_u64().ok()
429+
}) {
430+
flags.push((
431+
"_Self".to_owned(),
432+
Some(format!("[{}; {}]", self.tcx.type_of(def.did).to_string(), len)),
433+
));
434+
} else {
435+
flags.push((
436+
"_Self".to_owned(),
437+
Some(format!("[{}; _]", self.tcx.type_of(def.did).to_string())),
438+
));
439+
}
440+
}
441+
}
442+
413443
if let Ok(Some(command)) = OnUnimplementedDirective::of_item(
414444
self.tcx, trait_ref.def_id, def_id
415445
) {

0 commit comments

Comments
 (0)