Skip to content

Commit 00c92bd

Browse files
Check nested obligations during coercion unify
1 parent 23040c4 commit 00c92bd

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ use rustc_span::{self, BytePos, DesugaringKind, Span};
6262
use rustc_target::spec::abi::Abi;
6363
use rustc_trait_selection::infer::InferCtxtExt as _;
6464
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
65+
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
6566
use rustc_trait_selection::traits::{
6667
self, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt,
6768
};
@@ -144,12 +145,28 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
144145
debug!("unify(a: {:?}, b: {:?}, use_lub: {})", a, b, self.use_lub);
145146
self.commit_if_ok(|_| {
146147
let at = self.at(&self.cause, self.fcx.param_env);
147-
if self.use_lub {
148+
149+
let res = if self.use_lub {
148150
at.lub(DefineOpaqueTypes::Yes, b, a)
149151
} else {
150152
at.sup(DefineOpaqueTypes::Yes, b, a)
151153
.map(|InferOk { value: (), obligations }| InferOk { value: a, obligations })
154+
};
155+
156+
// In the new solver, lazy norm may allow us to shallowly equate
157+
// more types, but we emit possibly impossible-to-satisfy obligations.
158+
// Filter these cases out to make sure our coercion is more accurate.
159+
if self.tcx.trait_solver_next() {
160+
if let Ok(res) = &res {
161+
for obligation in &res.obligations {
162+
if !self.predicate_may_hold(&obligation) {
163+
return Err(TypeError::Mismatch);
164+
}
165+
}
166+
}
152167
}
168+
169+
res
153170
})
154171
}
155172

tests/ui/impl-trait/autoderef.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// revisions: current next
2+
//[next] compile-flag: -Ztrait-solver=next
13
// check-pass
24

35
use std::path::Path;

0 commit comments

Comments
 (0)