From 49a5d6da34e8840bbf6436e089051c63fdd4b672 Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Thu, 23 Jul 2026 16:04:48 +0330 Subject: [PATCH] fix: lowering of resolved const inference variables Signed-off-by: Amirhossein Akhlaghpour --- compiler/rustc_infer/src/infer/context.rs | 5 ++- .../src/canonical/mod.rs | 8 ++-- ...er-universe-resolved-const-issue-159703.rs | 19 +++++++++ ...niverse-resolved-const-issue-159703.stderr | 40 +++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 tests/ui/traits/next-solver/lower-universe-resolved-const-issue-159703.rs create mode 100644 tests/ui/traits/next-solver/lower-universe-resolved-const-issue-159703.stderr diff --git a/compiler/rustc_infer/src/infer/context.rs b/compiler/rustc_infer/src/infer/context.rs index d1d864246b9ff..9d5b51491e5e6 100644 --- a/compiler/rustc_infer/src/infer/context.rs +++ b/compiler/rustc_infer/src/infer/context.rs @@ -521,7 +521,10 @@ impl<'a, 'tcx> ty::TypeFolder> for LowerUniverseFolder<'a, 'tcx> { match c.kind() { ty::ConstKind::Infer(ty::InferConst::Var(vid)) => { let vid = self.infcx.root_const_var(vid); - let universe = self.infcx.try_resolve_const_var(vid).unwrap_err(); + let universe = match self.infcx.try_resolve_const_var(vid) { + Ok(value) => return value.fold_with(self), + Err(universe) => universe, + }; if self.for_universe.can_name(universe) { c } else { diff --git a/compiler/rustc_next_trait_solver/src/canonical/mod.rs b/compiler/rustc_next_trait_solver/src/canonical/mod.rs index f325862102318..2adb6a19cb6c3 100644 --- a/compiler/rustc_next_trait_solver/src/canonical/mod.rs +++ b/compiler/rustc_next_trait_solver/src/canonical/mod.rs @@ -392,12 +392,10 @@ where } let infcx = self.infcx; - // FIXME: make this a debug_assert. - // Currently proof tree evaluation can unify infer vars in original - // vars while not resolving them. - // See `tests/ui/traits/next-solver/transmute-from-async-closure.rs` + // Proof tree evaluation can unify inference variables in the original + // values without eagerly resolving them. let a = infcx.shallow_resolve_const(a); - debug_assert_eq!(b, infcx.shallow_resolve_const(b)); + let b = infcx.shallow_resolve_const(b); match (a.kind(), b.kind()) { ( ty::ConstKind::Infer(ty::InferConst::Var(a_vid)), diff --git a/tests/ui/traits/next-solver/lower-universe-resolved-const-issue-159703.rs b/tests/ui/traits/next-solver/lower-universe-resolved-const-issue-159703.rs new file mode 100644 index 0000000000000..56aff9211f789 --- /dev/null +++ b/tests/ui/traits/next-solver/lower-universe-resolved-const-issue-159703.rs @@ -0,0 +1,19 @@ +//@ compile-flags: -Znext-solver=globally +//@ edition: 2015 + +#![allow(bare_trait_objects)] + +trait Foo {} + +struct BarType; + +impl Foo for BarType {} +//~^ ERROR missing generics for struct `BarType` + +fn a(x: &Foo) { + let bar = BarType; + a(bar); + //~^ ERROR mismatched types +} + +fn main() {} diff --git a/tests/ui/traits/next-solver/lower-universe-resolved-const-issue-159703.stderr b/tests/ui/traits/next-solver/lower-universe-resolved-const-issue-159703.stderr new file mode 100644 index 0000000000000..3324d2a037c22 --- /dev/null +++ b/tests/ui/traits/next-solver/lower-universe-resolved-const-issue-159703.stderr @@ -0,0 +1,40 @@ +error[E0107]: missing generics for struct `BarType` + --> $DIR/lower-universe-resolved-const-issue-159703.rs:10:30 + | +LL | impl Foo for BarType {} + | ^^^^^^^ expected 1 generic argument + | +note: struct defined here, with 1 generic parameter: `N` + --> $DIR/lower-universe-resolved-const-issue-159703.rs:8:8 + | +LL | struct BarType; + | ^^^^^^^ -------------- +help: add missing generic argument + | +LL | impl Foo for BarType {} + | +++ + +error[E0308]: mismatched types + --> $DIR/lower-universe-resolved-const-issue-159703.rs:15:7 + | +LL | a(bar); + | - ^^^ expected `&dyn Foo`, found `BarType<_>` + | | + | arguments to this function are incorrect + | + = note: expected reference `&dyn Foo` + found struct `BarType<_>` +note: function defined here + --> $DIR/lower-universe-resolved-const-issue-159703.rs:13:4 + | +LL | fn a(x: &Foo) { + | ^ ------- +help: consider borrowing here + | +LL | a(&bar); + | + + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0107, E0308. +For more information about an error, try `rustc --explain E0107`.