segfault Rust::TyTy::BaseType::get_ref()
auto-reduced (treereduce-rust):
//@compile-flags: -frust-incomplete-and-experimental-compiler-do-not-use -frust-crate-attr=no_core
pub fn check(buf: &[u8]) -> bool {
match (buf[3] | 0x20, buf[0] | 0x20, buf[2], buf[3]) {
(b'a', b'b', b'+', b'-') => true,
}
}
original:
pub fn check(buf: &[u8]) -> bool {
if buf.len() < 4 {
return false;
}
match (buf[3] | 0x20, buf[0] | 0x20, buf[2], buf[3]) {
(b'a', b'b', b'+', b'-') => {
true
},
_ => {
false
}
}
}
pub fn check_case_sensitive(buf: &[u8]) -> bool {
if buf.len() < 4 {
return false;
}
match (buf[0], buf[1], buf[2], buf[3]) {
(b'a', b'b', b'+', b'-') => {
true
},
(b'a', b'+', b'+', b'-') => {
if buf.len() < 4 {
return false;
}
match (buf[0] | 0x20, buf[1] | 0x20, buf[2], buf[3]) {
(b'a', b'b', b'+', b'-') => {
true
},
_ => {
false
}
}
}
}
}
Version information:
a02e9a643d591708a5de6d506ef75f3405a91a0e
Command:
/home/matthias/vcs/github/gccrs/gccrs-build/gcc/crab1 -frust-incomplete-and-experimental-compiler-do-not-use -frust-crate-attr=no_core -frust-crate-attr=feature(no_core)
Program output
/tmp/icemaker_global_tempdir.gDdyZYBrYJLi/rustc_testrunner_tmpdir_reporting.uOGrzVUiHn8k/mvce.rs:2:12: error: the type ‘& [u8]’ cannot be indexed by ‘<integer>’ [E0277]
2 | match (buf[3] | 0x20, buf[0] | 0x20, buf[2], buf[3]) {
| ^~~ ~
/tmp/icemaker_global_tempdir.gDdyZYBrYJLi/rustc_testrunner_tmpdir_reporting.uOGrzVUiHn8k/mvce.rs:2:12: error: cannot apply operator ‘|’ to types <tyty::error> and <integer>
/tmp/icemaker_global_tempdir.gDdyZYBrYJLi/rustc_testrunner_tmpdir_reporting.uOGrzVUiHn8k/mvce.rs:2:27: error: the type ‘& [u8]’ cannot be indexed by ‘<integer>’ [E0277]
2 | match (buf[3] | 0x20, buf[0] | 0x20, buf[2], buf[3]) {
| ^~~ ~
/tmp/icemaker_global_tempdir.gDdyZYBrYJLi/rustc_testrunner_tmpdir_reporting.uOGrzVUiHn8k/mvce.rs:2:27: error: cannot apply operator ‘|’ to types <tyty::error> and <integer>
/tmp/icemaker_global_tempdir.gDdyZYBrYJLi/rustc_testrunner_tmpdir_reporting.uOGrzVUiHn8k/mvce.rs:2:42: error: the type ‘& [u8]’ cannot be indexed by ‘<integer>’ [E0277]
2 | match (buf[3] | 0x20, buf[0] | 0x20, buf[2], buf[3]) {
| ^~~ ~
/tmp/icemaker_global_tempdir.gDdyZYBrYJLi/rustc_testrunner_tmpdir_reporting.uOGrzVUiHn8k/mvce.rs:2:50: error: the type ‘& [u8]’ cannot be indexed by ‘<integer>’ [E0277]
2 | match (buf[3] | 0x20, buf[0] | 0x20, buf[2], buf[3]) {
| ^~~ ~
crab1: internal compiler error: Segmentation fault
0x35dee47 internal_error(char const*, ...)
../../gcc/diagnostic-global-context.cc:787
0x1b9a97f crash_signal
../../gcc/toplev.cc:325
0x110f580 Rust::TyTy::BaseType::get_ref() const
../../gcc/rust/typecheck/rust-tyty.cc:169
0x112a8bc Rust::TyTy::TyWithLocation::TyWithLocation(Rust::TyTy::BaseType*)
../../gcc/rust/typecheck/rust-tyty-util.cc:138
0x11a7c57 Rust::Resolver::TypeCheckPattern::visit(Rust::HIR::LiteralPattern&)
../../gcc/rust/typecheck/rust-hir-type-check-pattern.cc:745
0x11a89ef Rust::Resolver::TypeCheckPattern::Resolve(Rust::HIR::Pattern&, Rust::TyTy::BaseType*)
../../gcc/rust/typecheck/rust-hir-type-check-pattern.cc:40
0x11adf1a Rust::Resolver::TypeCheckPattern::visit(Rust::HIR::TuplePattern&)
../../gcc/rust/typecheck/rust-hir-type-check-pattern.cc:662
0x11a89ef Rust::Resolver::TypeCheckPattern::Resolve(Rust::HIR::Pattern&, Rust::TyTy::BaseType*)
../../gcc/rust/typecheck/rust-hir-type-check-pattern.cc:40
0x11b42ca Rust::Resolver::TypeCheckExpr::visit(Rust::HIR::MatchExpr&)
../../gcc/rust/typecheck/rust-hir-type-check-expr.cc:1852
0x11b1220 Rust::Resolver::TypeCheckExpr::Resolve(Rust::HIR::Expr&)
../../gcc/rust/typecheck/rust-hir-type-check-expr.cc:51
0x11b35bc Rust::Resolver::TypeCheckExpr::visit(Rust::HIR::BlockExpr&)
../../gcc/rust/typecheck/rust-hir-type-check-expr.cc:752
0x11b1220 Rust::Resolver::TypeCheckExpr::Resolve(Rust::HIR::Expr&)
../../gcc/rust/typecheck/rust-hir-type-check-expr.cc:51
0x118f0af Rust::Resolver::TypeCheckItem::visit(Rust::HIR::Function&)
../../gcc/rust/typecheck/rust-hir-type-check-item.cc:1020
0x117bb3c Rust::Resolver::TypeCheckItem::Resolve(Rust::HIR::Item&)
../../gcc/rust/typecheck/rust-hir-type-check-item.cc:190
0x10ff69b Rust::Resolver::TypeResolution::Resolve(Rust::HIR::Crate&)
../../gcc/rust/typecheck/rust-hir-type-check.cc:72
0xf82bca Rust::Session::compile_crate(char const*)
../../gcc/rust/rust-session-manager.cc:812
/home/matthias/vcs/github/gccrs/gccrs-build/gcc/crab1 /tmp/icemaker_global_tempdir.gDdyZYBrYJLi/rustc_testrunner_tmpdir_reporting.uOGrzVUiHn8k/mvce.rs -o/tmp/icemaker_global_tempdir.gDdyZYBrYJLi/rustc_testrunner_tmpdir_reporting.uOGrzVUiHn8k/binaryname -frust-incomplete-and-experimental-compiler-do-not-use -frust-crate-attr=no_core -frust-crate-attr=feature(no_core)
Please submit a full bug report, with preprocessed source (by using -freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Hi Diyan here, I would like to work on this issue.
From the provided reduced test case, it appears that the compiler first encounters type-checking errors while resolving indexing expressions on &[u8], and subsequently crashes with a segmentation fault during pattern type checking.
The backtrace points to Rust::Resolver::TypeCheckPattern::visit(HIR::LiteralPattern&) in gcc/rust/typecheck/rust-hir-type-check-pattern.cc, specifically around the handling of literal patterns when the expected type is already erroneous.
I'll investigate how error types are propagated into pattern type checking and look into preventing the compiler from dereferencing an invalid TyTy::BaseType instead of reporting the original diagnostics gracefully.
I'll work on a regression test alongside the fix.