Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c4e05e5

Browse files
committed
Auto merge of #136988 - compiler-errors:impossible_predicates, r=lcnr
Use the new solver in the `impossible_predicates` The old solver is unsound for many reasons. One of which was weaponized by `@lcnr` in #140212, where the old solver was incompletely considering a dyn vtable method to be impossible and replacing its vtable entry with a null value. This null function could be called post-mono. The new solver is expected to be less incomplete due to its correct handling of higher-ranked aliases in relate. This PR switches the `impossible_predicates` query to use the new solver, which patches this UB. r? lcnr
2 parents d163a28 + 257f687 commit c4e05e5

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

‎compiler/rustc_trait_selection/src/traits/mod.rs‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,11 @@ fn replace_param_and_infer_args_with_placeholder<'tcx>(
690690
/// used during analysis.
691691
pub fn impossible_predicates<'tcx>(tcx: TyCtxt<'tcx>, predicates: Vec<ty::Clause<'tcx>>) -> bool {
692692
debug!("impossible_predicates(predicates={:?})", predicates);
693-
let (infcx, param_env) =
694-
tcx.infer_ctxt().build_with_typing_env(ty::TypingEnv::fully_monomorphized());
693+
let (infcx, param_env) = tcx
694+
.infer_ctxt()
695+
.with_next_trait_solver(true)
696+
.build_with_typing_env(ty::TypingEnv::fully_monomorphized());
697+
695698
let ocx = ObligationCtxt::new(&infcx);
696699
let predicates = ocx.normalize(&ObligationCause::dummy(), param_env, predicates);
697700
for predicate in predicates {
@@ -704,13 +707,6 @@ pub fn impossible_predicates<'tcx>(tcx: TyCtxt<'tcx>, predicates: Vec<ty::Clause
704707
return true;
705708
}
706709

707-
// Leak check for any higher-ranked trait mismatches.
708-
// We only need to do this in the old solver, since the new solver already
709-
// leak-checks.
710-
if !infcx.next_trait_solver() && infcx.leak_check(ty::UniverseIndex::ROOT, None).is_err() {
711-
return true;
712-
}
713-
714710
false
715711
}
716712

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ run-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
trait Id {
7+
type This<'a>;
8+
}
9+
impl<T> Id for T {
10+
type This<'a> = T;
11+
}
12+
13+
trait Trait<T> {}
14+
impl<T: Id> Trait<for<'a> fn(T::This<'a>)> for T {}
15+
16+
trait Method<T: Id> {
17+
fn call_me(&self)
18+
where
19+
T: Trait<for<'a> fn(T::This<'a>)>;
20+
}
21+
22+
impl<T, U> Method<U> for T {
23+
fn call_me(&self) {
24+
println!("method was reachable");
25+
}
26+
}
27+
28+
fn generic<T: Id>(x: &dyn Method<T>) {
29+
// Proving `T: Trait<for<'a> fn(T::This<'a>)>` holds.
30+
x.call_me();
31+
}
32+
33+
fn main() {
34+
// Proving `u32: Trait<fn(u32)>` fails due to incompleteness.
35+
// We don't add the method to the vtable of `dyn Method`, so
36+
// calling it causes UB.
37+
generic::<u32>(&());
38+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /