Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modules with same names as built in types causes ICE #3315

Open
matthewjasper opened this issue Dec 21, 2024 · 7 comments
Open

Modules with same names as built in types causes ICE #3315

matthewjasper opened this issue Dec 21, 2024 · 7 comments

Comments

@matthewjasper
Copy link
Contributor

Code

mod i32 {}

Meta

Version: gccrs (Compiler-Explorer-Build-gcc-b5c354d038f800695a8d730c56c4a4f744134adb-binutils-2.42) 14.0.1 20240309 (experimental)

Error output

crab1: internal compiler error: in setup_builtin_types, at rust/resolve/rust-late-name-resolver-2.0.cc:97
0x27eb5ec internal_error(char const*, ...)
	???:0
0xaf6cc9 fancy_abort(char const*, int, char const*)
	???:0
0xdbb522 Rust::Resolver2_0::Late::go(Rust::AST::Crate&)
	???:0
0xc4bef0 Rust::Session::compile_crate(char const*)
	???:0
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.
Compiler returned: 1
@liamnaddell
Copy link
Contributor

From reading here: https://doc.rust-lang.org/reference/names/namespaces.html

builtins and modules both belong in the Types namespace, however, that can't really be correct, since rustc allows you to name a module i32. I guess the idea is that builtins are resolved separately from other types? Otherwise, how would you prevent NR conflicts here?

@liamnaddell
Copy link
Contributor

liamnaddell commented Feb 3, 2025

Also, look at this error:

liam@newgame ~/projects/gccrs/gccrs-build $ rustc main.rs
...
error[E0605]: non-primitive cast: `i32` as `i32`
 --> main.rs:4:18
  |
4 |     let a: i32 = 0 as i32;
  |                  ^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error: aborting due to 1 previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0605`.
liam@newgame ~/projects/gccrs/gccrs-build $ cat main.rs
struct i32 {}

fn main() {
    let a: i32 = 0 as i32;
}
liam@newgame ~/projects/gccrs/gccrs-build $

What happens here is that i32 gets shadowed to the struct, which causes a compile error, since i32 no longer refers to a primative type.

@liamnaddell
Copy link
Contributor

liamnaddell commented Feb 3, 2025

I guess what we are expected to do is remove builtin types from the resolution context when another Item overshadows it

EDIT: I tried this, but then i32 doesn't exist, and this rustc code (which is supposed to compile) no longer compiles:

mod i32 {}

fn main() {
    let a: i32 = 0 as i32;
}

@liamnaddell
Copy link
Contributor

liamnaddell commented Feb 3, 2025

Modules are clearly handled separately from other types like structs. I think modules are somehow part of their own namespace?

EDIT: I checked rustc, structs, builtins, modules, etc are all in the Types namespace, at least at rustc's HIR level. They end up going into different RIB's though. I'm not really sure how you'd make this work yet though

@powerboat9
Copy link
Collaborator

It looks like builtins are really supposed to go in a language prelude. That way, module definitions can shadow builtin definitions by virtue of being in a different scope.

@liamnaddell
Copy link
Contributor

It looks like builtins are really supposed to go in a language prelude. That way, module definitions can shadow builtin definitions by virtue of being in a different scope.

Would this be part of a distinct RIB?

@powerboat9
Copy link
Collaborator

powerboat9 commented Feb 3, 2025

Yep. Fixing this would probably entail some adjustments to the ForeverStack node hierarchy, and some code for handling descent into prelude nodes (since we'll want multiple preludes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

4 participants