Skip to content

Commit

Permalink
Rollup merge of rust-lang#117416 - compiler-errors:tait-in-bad-body, …
Browse files Browse the repository at this point in the history
…r=oli-obk

Also consider TAIT to be uncomputable if the MIR body is tainted

Not totally sure if this is the best solution. We could, alternatively, look at the hir typeck results and try to take a type from there instead of just falling back to type error, inferring `u8` instead of `{type error}`. Not certain it really matters, though.

Happy to iterate on this.

Fixes rust-lang#117413

r? `@oli-obk` cc `@Nadrieril`
  • Loading branch information
matthiaskrgr authored Oct 31, 2023
2 parents ffb7ed9 + 48491c1 commit b7acdff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
14 changes: 11 additions & 3 deletions compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,17 @@ impl TaitConstraintLocator<'_> {
};

// Use borrowck to get the type with unerased regions.
let concrete_opaque_types = &self.tcx.mir_borrowck(item_def_id).concrete_opaque_types;
debug!(?concrete_opaque_types);
if let Some(&concrete_type) = concrete_opaque_types.get(&self.def_id) {
let borrowck_results = &self.tcx.mir_borrowck(item_def_id);

// If the body was tainted, then assume the opaque may have been constrained and just set it to error.
if let Some(guar) = borrowck_results.tainted_by_errors {
self.found =
Some(ty::OpaqueHiddenType { span: DUMMY_SP, ty: Ty::new_error(self.tcx, guar) });
return;
}

debug!(?borrowck_results.concrete_opaque_types);
if let Some(&concrete_type) = borrowck_results.concrete_opaque_types.get(&self.def_id) {
debug!(?concrete_type, "found constraint");
if let Some(prev) = &mut self.found {
if concrete_type.ty != prev.ty && !(concrete_type, prev.ty).references_error() {
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/type-alias-impl-trait/unconstrained-due-to-bad-pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![feature(type_alias_impl_trait)]

type Tait = impl Copy;
// Make sure that this TAIT isn't considered unconstrained...

fn empty_opaque() -> Tait {
if false {
match empty_opaque() {}
//~^ ERROR non-empty
}
0u8
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0004]: non-exhaustive patterns: type `Tait` is non-empty
--> $DIR/unconstrained-due-to-bad-pattern.rs:8:15
|
LL | match empty_opaque() {}
| ^^^^^^^^^^^^^^
|
= note: the matched value is of type `Tait`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
|
LL ~ match empty_opaque() {
LL + _ => todo!(),
LL + }
|

error: aborting due to previous error

For more information about this error, try `rustc --explain E0004`.

0 comments on commit b7acdff

Please sign in to comment.