Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/rustc_infer/src/infer/snapshot/fudge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
ty
}
}
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
unreachable!("unexpected fresh infcx var")
}
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => ty,
}
} else if ty.has_infer() {
ty.super_fold_with(self)
Expand Down Expand Up @@ -247,7 +245,9 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
}
}
ty::InferConst::Fresh(_) => {
unreachable!("unexpected fresh infcx var")
// `Fresh` const vars are produced by `TypeFreshener` and don't refer to infcx state.
// They can safely be forwarded through `fudge_inference_if_ok`.
ct
}
}
} else if ct.has_infer() {
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,10 +653,12 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
// into the witness.
ty::CoroutineWitness(..) => (),

ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => (),

// These variants should not exist as a self type.
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
| ty::Param(_)
| ty::Bound(_, _) => bug!("unexpected self type: {self_ty}"),
ty::Infer(ty::TyVar(_)) | ty::Param(_) | ty::Bound(_, _) => {
bug!("unexpected self type: {self_ty}")
}
}

#[allow(rustc::usage_of_type_ir_traits)]
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,10 @@ where
| ty::Placeholder(..)
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
| ty::Error(_) => return,
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
return;
}
ty::Bound(..) => {
panic!("unexpected self type for `{goal:?}`")
}

Expand Down Expand Up @@ -799,6 +802,14 @@ where
}

let self_ty = goal.predicate.self_ty();
// FreshTy is used as trait_object_dummy_self and should not reach here
if matches!(
self_ty.kind(),
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
) {
return;
}

let bounds = match self_ty.kind() {
ty::Bool
| ty::Char
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ where

ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) => Err(NoSolution),

ty::Bound(..)
| ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
// FreshTy is used as trait_object_dummy_self and should not reach here
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => Err(NoSolution),

ty::Bound(..) | ty::Infer(ty::TyVar(_)) => {
panic!("unexpected type `{ty:?}`")
}

Expand Down
11 changes: 10 additions & 1 deletion compiler/rustc_trait_selection/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,16 @@ pub fn sizedness_fast_path<'tcx>(
_ => return false,
};

if trait_pred.self_ty().has_trivial_sizedness(tcx, sizedness) {
let self_ty = trait_pred.self_ty();
// FreshTy is used as trait_object_dummy_self and should not reach has_trivial_sizedness
if matches!(
self_ty.kind(),
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
) {
return false;
}

if self_ty.has_trivial_sizedness(tcx, sizedness) {
debug!("fast path -- trivial sizedness");
return true;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/traits/next-solver/fresh-ty-has-trivial-sizedness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Fixes #151709
//@ compile-flags: -Znext-solver=globally

trait A {
type Ty;
const CT: Self::Ty;
}

fn main() {
let _: &dyn A<Ty = i32, CT = 0> = &();
//~^ ERROR associated const equality is incomplete
//~| ERROR the trait `A` is not dyn compatible
//~| ERROR the size for values of type `FreshTy(0)` cannot be known
//~| ERROR the trait bound `FreshTy(0): A` is not satisfied
}
59 changes: 59 additions & 0 deletions tests/ui/traits/next-solver/fresh-ty-has-trivial-sizedness.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
error[E0658]: associated const equality is incomplete
--> $DIR/fresh-ty-has-trivial-sizedness.rs:12:29
|
LL | let _: &dyn A<Ty = i32, CT = 0> = &();
| ^^^^^^
|
= note: see issue #132980 <https://github.com/rust-lang/rust/issues/132980> for more information
= help: add `#![feature(min_generic_const_args)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0038]: the trait `A` is not dyn compatible
--> $DIR/fresh-ty-has-trivial-sizedness.rs:12:17
|
LL | let _: &dyn A<Ty = i32, CT = 0> = &();
| ^^^^^^^^^^^^^^^^^^^ `A` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
--> $DIR/fresh-ty-has-trivial-sizedness.rs:8:11
|
LL | trait A {
| - this trait is not dyn compatible...
LL | type Ty;
LL | const CT: Self::Ty;
| ^^ ...because it contains this associated `const`
= help: consider moving `CT` to another trait

error[E0277]: the size for values of type `FreshTy(0)` cannot be known
--> $DIR/fresh-ty-has-trivial-sizedness.rs:12:34
|
LL | let _: &dyn A<Ty = i32, CT = 0> = &();
| ^ doesn't have a known size
|
= help: the nightly-only, unstable trait `MetaSized` is not implemented for `FreshTy(0)`
note: required by a bound in `A`
--> $DIR/fresh-ty-has-trivial-sizedness.rs:6:1
|
LL | / trait A {
LL | | type Ty;
LL | | const CT: Self::Ty;
LL | | }
| |_^ required by this bound in `A`

error[E0277]: the trait bound `FreshTy(0): A` is not satisfied
--> $DIR/fresh-ty-has-trivial-sizedness.rs:12:34
|
LL | let _: &dyn A<Ty = i32, CT = 0> = &();
| ^ the trait `A` is not implemented for `FreshTy(0)`
|
help: this trait has no implementations, consider adding one
--> $DIR/fresh-ty-has-trivial-sizedness.rs:6:1
|
LL | trait A {
| ^^^^^^^

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0038, E0277, E0658.
For more information about an error, try `rustc --explain E0038`.
Loading