1
Fork 0

Add note to non-exhaustive match on reference to empty

Rust prints "type `&A` is non-empty" even is A is empty.
This is the intended behavior, but can be confusing.
This commit adds a note to non-exhaustive pattern errors if they are a
reference to something uninhabited.

I did not add tests to check that the note is not shown for
non-references or inhabited references, because this is already done
in other tests.

Maybe the added test is superfluous, because
`always-inhabited-union-ref` already checks for this case.

This does not handle &&Void or &&&void etc. I could add those as special
cases as well and ignore people who need quadruple
references.

Fixes #78123
This commit is contained in:
Daniel Noom 2021-01-03 14:13:33 +01:00
parent 18cb4ad3b9
commit 998bf0ab88
4 changed files with 29 additions and 0 deletions

View file

@ -503,6 +503,11 @@ fn non_exhaustive_match<'p, 'tcx>(
));
}
}
if let ty::Ref(_, sub_ty, _) = scrut_ty.kind() {
if cx.tcx.is_ty_uninhabited_from(cx.module, sub_ty, cx.param_env) {
err.note("references are always considered inhabited");
}
}
err.emit();
}