Skip to content
Merged
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
10 changes: 7 additions & 3 deletions compiler/rustc_trait_selection/src/traits/query/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,14 @@ impl<'a, 'tcx> QueryNormalizer<'a, 'tcx> {
// `tcx.normalize_canonicalized_projection` may normalize to a type that
// still has unevaluated consts, so keep normalizing here if that's the case.
// Similarly, `tcx.normalize_canonicalized_free_alias` will only unwrap one layer
// of type and we need to continue folding it to reveal the TAIT behind it.
// of type/const and we need to continue folding it to reveal the TAIT behind it
// or further normalize nested unevaluated consts.
if res != term.to_term(tcx)
&& (res.as_type().map_or(false, |t| t.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION))
|| term.kind(tcx) == ty::AliasTermKind::FreeTy)
&& (res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION)
|| matches!(
term.kind(tcx),
ty::AliasTermKind::FreeTy | ty::AliasTermKind::FreeConst
))
Comment on lines 381 to +386
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this to just be if term.has_aliases() and I don't understand why it isn't. Can you try changing it to that and see if anything breaks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think term.has_aliases() is not a proper solution (though I didn't know that API). Actually it caused a SEGV during stage1 lib building:

output (extracted)
Building stage1 library artifacts (stage1 -> stage1, x86_64-unknown-linux-gnu)
   Compiling compiler_builtins v0.1.160 (/home/lapla/repos/rust/library/compiler-builtins/compiler-builtins)
   Compiling core v0.0.0 (/home/lapla/repos/rust/library/core)
   Compiling libc v0.2.178
   Compiling object v0.37.3
   Compiling std v0.0.0 (/home/lapla/repos/rust/library/std)
   Compiling test v0.0.0 (/home/lapla/repos/rust/library/test)
error: rustc interrupted by SIGSEGV, printing backtrace

/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(+0x93ee8c) [0x7bd87833ee8c]
/usr/lib/x86_64-linux-gnu/libc.so.6(+0x45f30) [0x7bd877645f30]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(+0x313ad45) [0x7bd87ab3ad45]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(+0x32f10d9) [0x7bd87acf10d9]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(+0x335e113) [0x7bd87ad5e113]

### cycle encountered after 5 frames with period 6
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(_RNvXs_NtNtNtCsiiExf7IvESp_21rustc_trait_selection6traits5query9normalizeNtB4_15QueryNormalizerINtNtCs44sggCrMDsM_13rustc_type_ir4fold18FallibleTypeFolderNtNtNtCsforUfnDCdfO_12rustc_middle2ty7context6TyCtxtE11try_fold_ty+0x1ca) [0x7bd87ad71e1a]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(+0x335e82e) [0x7bd87ad5e82e]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(_RNvXs_NtNtNtCsiiExf7IvESp_21rustc_trait_selection6traits5query9normalizeNtB4_15QueryNormalizerINtNtCs44sggCrMDsM_13rustc_type_ir4fold18FallibleTypeFolderNtNtNtCsforUfnDCdfO_12rustc_middle2ty7context6TyCtxtE11try_fold_ty+0x1ca) [0x7bd87ad71e1a]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(+0x335e82e) [0x7bd87ad5e82e]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(_RNvXs_NtNtNtCsiiExf7IvESp_21rustc_trait_selection6traits5query9normalizeNtB4_15QueryNormalizerINtNtCs44sggCrMDsM_13rustc_type_ir4fold18FallibleTypeFolderNtNtNtCsforUfnDCdfO_12rustc_middle2ty7context6TyCtxtE11try_fold_ty+0x1ca) [0x7bd87ad71e1a]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(+0x335e82e) [0x7bd87ad5e82e]
### recursed 41 times

/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(_RNvXs_NtNtNtCsiiExf7IvESp_21rustc_trait_selection6traits5query9normalizeNtB4_15QueryNormalizerINtNtCs44sggCrMDsM_13rustc_type_ir4fold18FallibleTypeFolderNtNtNtCsforUfnDCdfO_12rustc_middle2ty7context6TyCtxtE11try_fold_ty+0x1ca) [0x7bd87ad71e1a]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(+0x335e82e) [0x7bd87ad5e82e]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(_RNvXs_NtNtNtCsiiExf7IvESp_21rustc_trait_selection6traits5query9normalizeNtB4_15QueryNormalizerINtNtCs44sggCrMDsM_13rustc_type_ir4fold18FallibleTypeFolderNtNtNtCsforUfnDCdfO_12rustc_middle2ty7context6TyCtxtE11try_fold_ty+0x1ca) [0x7bd87ad71e1a]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(+0x335e82e) [0x7bd87ad5e82e]
/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1/lib/librustc_driver-2ff71906bfdf6a94.so(_RNvXs_NtNtNtCsiiExf7IvESp_21rustc_trait_selection6traits5query9normalizeNtB4_15QueryNormalizerINtNtCs44sggCrMDsM_13rustc_type_ir4fold18FallibleTypeFolderNtNtNtCsforUfnDCdfO_12rustc_middle2ty7context6TyCtxtE11try_fold_ty+0x1ca) [0x7bd87ad71e1a]

note: rustc unexpectedly overflowed its stack! this is a bug
note: maximum backtrace depth reached, frames may have been lost
note: we would appreciate a report at https://github.com/rust-lang/rust
help: you can increase rustc's stack size by setting RUST_MIN_STACK=16777216
note: backtrace dumped due to SIGSEGV! resuming signal
rustc exited with signal: 11 (SIGSEGV) (core dumped)
error: could not compile `core` (lib)

Caused by:
  process didn't exit successfully: `/home/lapla/repos/rust/build/bootstrap/debug/rustc /home/lapla/repos/rust/build/bootstrap/debug/rustc --crate-name core --edition=2024 library/core/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=130 --crate-type lib --emit=dep-info,metadata,link -Z embed-metadata=no -C opt-level=3 -C embed-bitcode=no -C codegen-units=1 --warn=unexpected_cfgs --check-cfg 'cfg(no_fp_fmt_parse)' --check-cfg 'cfg(feature, values(any()))' --check-cfg 'cfg(target_has_reliable_f16)' --check-cfg 'cfg(target_has_reliable_f16_math)' --check-cfg 'cfg(target_has_reliable_f128)' --check-cfg 'cfg(target_has_reliable_f128_math)' --check-cfg 'cfg(llvm_enzyme)' -Cembed-bitcode=yes -Zunstable-options -Cforce-frame-pointers=non-leaf --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values("debug_refcell", "llvm_enzyme", "optimize_for_size", "panic_immediate_abort"))' -C metadata=d728f96fea50222e -C extra-filename=-15fde4dd13344f6e --out-dir /home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/deps --target x86_64-unknown-linux-gnu -C linker=clang -L dependency=/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/deps -L dependency=/home/lapla/repos/rust/build/x86_64-unknown-linux-gnu/stage1-std/dist/deps -Zannotate-moves '--check-cfg=cfg(feature,values(any()))' -Zunstable-options -Zmacro-backtrace -Csplit-debuginfo=off -Cprefer-dynamic -Zinline-mir -Zinline-mir-preserve-debug -Zmir_strip_debuginfo=locals-in-tiny-functions -Clink-args=-Wl,-z,origin '-Clink-args=-Wl,-rpath,$ORIGIN/../lib' -Alinker-messages '-Zcrate-attr=doc(html_root_url="https://doc.rust-lang.org/nightly/")' -Z binary-dep-depinfo` (exit status: 254)
Build completed unsuccessfully in 0:04:56

HAS_ALIAS includes HAS_TY_OPAQUE, and opaque types are not normalized outside of TypingMode::PostAnalysis, so the res!=term.to_term(tcx) condition keeps passing and the recursion never terminates.

I also found needs_normalization(), which excludes HAS_TY_OPAQUE depending on the typing mode, but it still includes HAS_TY_PROJECTION and others, which similarly caused a stack overflow. Since projection types are already handled directly by try_fold_ty, the re-normalization here should only need HAS_CT_PROJECTION and the FreeTy/FreeConst cases (as normalize_canonicalized_free_alias() only unwraps one layer) I think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks :3

{
res.try_fold_with(self)
} else {
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/generic-const-items/type-const-nested-assoc-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ check-pass

#![feature(generic_const_items, min_generic_const_args)]
#![allow(incomplete_features)]

type const CT<T: ?Sized>: usize = { <T as Trait>::N };

trait Trait {
type const N: usize;
}

impl<T: ?Sized> Trait for T {
type const N:usize = 0;
}

fn f(_x: [(); CT::<()>]) {}

fn main() {}
Loading