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

Fix modules with same name as builtins causing ICE (#3315) #3437

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

liamnaddell
Copy link
Contributor

@liamnaddell liamnaddell commented Feb 14, 2025

@powerboat9 I can update to move the language prelude to rust-forever-stack.hxx if you still want the change

gcc/rust/ChangeLog:

	* resolve/rust-forever-stack.h: Add a dedicated prelude node for the Language prelude
	* resolve/rust-forever-stack.hxx: Add support code for the prelude node
	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Move language prelude builtins to the prelude context
	* resolve/rust-name-resolution-context.cc: Add code for handling the prelude corner case
	* resolve/rust-rib.h: Add a special Prelude rib type

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3315-1.rs: Add test for module with same name as builtin
	* rust/compile/issue-3315-2.rs: Test with utilization of i32 type
	* rust/compile/nr2/exclude: issue-3315-2.rs Does not work with NR2.0

Thank you for making Rust GCC better!

If your PR fixes an issue, you can add "Fixes #issue_number" into this
PR description and the git commit message. This way the issue will be
automatically closed when your PR is merged. If your change addresses
an issue but does not fully fix it please mark it as "Addresses #issue_number"
in the git commit message.

Here is a checklist to help you with your PR.

Note that you can skip the above if you are just opening a WIP PR in
order to get feedback.

*Please write a comment explaining your change. This is the message
that will be part of the merge commit.

@liamnaddell liamnaddell force-pushed the modname branch 5 times, most recently from 44e6e4e to 98f7902 Compare February 15, 2025 18:42
// we're at the root :) hopefully! Globbed declaration allows
// user-defined items in the `types` namespace
// to shadow builtins. Probably unwise, but allowed by rustc!
auto ok = ctx.types.insert_globbed (builtin.name, builtin.node_id);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since it's in a separate rib, insert, insert_shadowable, or insert_globbed would all work fine here

@@ -107,6 +107,11 @@ NameResolutionContext::scoped (Rib::Kind rib_kind, NodeId id,
std::function<void (void)> lambda,
tl::optional<Identifier> path)
{
// Prelude doesn't have an access path
// TODO: Assert current cursor is at the root
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should already hit an assertion, so you could remove the comment or add another assertion

// Prelude doesn't have an access path
// TODO: Assert current cursor is at the root
if (rib_kind == Rib::Kind::Prelude)
rust_assert (!path);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be moved to push_inner or copied there, but an assertion like this should probably be in push_inner

@powerboat9
Copy link
Collaborator

@powerboat9 I can update to move the language prelude to rust-forever-stack.hxx if you still want the change

It's alright, it looks good

@liamnaddell liamnaddell force-pushed the modname branch 2 times, most recently from c9a02b2 to 20e456a Compare February 15, 2025 22:53
@liamnaddell
Copy link
Contributor Author

@powerboat9 Thank you for the reviews, and sorry about the TODO: comments -- I forgot about those.

I think I fixed all of the issues you pointed out.

Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

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

Looks like a regression for:

Running /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/borrowck/borrowck.exp ...
Running /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/compile.exp ...
Running /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/macros/builtin/builtin_macro.exp ...
Running /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/macros/mbe/mbe_macro.exp ...
Running /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/macros/proc/proc_macro.exp ...
Running /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/nr2/compile.exp ...
FAIL: /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/macros/mbe/macro54.rs: nr2 failure: FAIL: rust/compile/macros/mbe/macro54.rs -frust-name-resolution-2.0 (test for excess errors)

@liamnaddell
Copy link
Contributor Author

Running /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/macros/mbe/mbe_macro.exp ...
Running /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/macros/proc/proc_macro.exp ...
Running /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/nr2/compile.exp ...
FAIL: /home/runner/work/gccrs/gccrs/gcc/testsuite/rust/compile/macros/mbe/macro54.rs: nr2 failure: FAIL: rust/compile/macros/mbe/macro54.rs -frust-name-resolution-2.0 (test for excess errors)

For some reason after rebasing these fail now. I'll take a look at these

@liamnaddell
Copy link
Contributor Author

I think the strategy I chose to implement this doesn't work, since the test involves resolving something like u32::VALUE.

I'll try to re-implement

@liamnaddell liamnaddell force-pushed the modname branch 5 times, most recently from a865b40 to dbc0b7d Compare March 2, 2025 20:48
@liamnaddell
Copy link
Contributor Author

@powerboat9 @philberty I added some code to get_segments to handle the prelude rib as a special case. I think my PR should work now

Copy link
Collaborator

@powerboat9 powerboat9 left a comment

Choose a reason for hiding this comment

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

Otherwise, looks great

{
// If you push_inner into the prelude from outside the root, you will pop
// back into the
// root, which could screw up a traversal.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should probably adjust the formatting on this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -487,6 +519,7 @@ ForeverStack<N>::resolve_segments (
*
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should probably add an entry to the numbered list in the above comment, mentioning changing searched_prelude from false to true

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 added a comment for this, let me know if it needs modification.

* It has the root node as a parent, and acts as a "special case" for name
* resolution
*/
Node prelude;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I probably should have noticed and commented on this earlier, but this should really be called something like language_prelude, given that we'll have to deal with multiple preludes

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 considered that as well, I figured that we would also dump the std::prelude and core::prelude in here as well

Copy link
Collaborator

Choose a reason for hiding this comment

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

I figured we'd keep some sort of reference to the standard library prelude -- since that should already be a module with its own Node, we shouldn't need to try to copy entries into another Node

Copy link
Contributor Author

@liamnaddell liamnaddell Mar 2, 2025

Choose a reason for hiding this comment

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

I think the comment there clarifies that it's currently only used for the language prelude (language builtins). If it needs to be beefed up with a better explanation I can do that.

@@ -107,6 +107,7 @@ NameResolutionContext::scoped (Rib::Kind rib_kind, NodeId id,
std::function<void (void)> lambda,
tl::optional<Identifier> path)
{
// NOTE: You should be at the root node when calling this function.
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should be at the root node, if pushing a Prelude rib kind

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦

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'll update to clarify that we only need to be at the root for the prelude rib pushes

@powerboat9
Copy link
Collaborator

powerboat9 commented Mar 2, 2025

It looks like rustc has some backtracking capability. We may have to implement something like that later, for full parity, but that's probably outside the scope of this PR

@liamnaddell liamnaddell requested a review from philberty March 5, 2025 01:14
gcc/rust/ChangeLog:

	* resolve/rust-forever-stack.h: Add a dedicated prelude node for
	the Language prelude
	* resolve/rust-forever-stack.hxx: Add support code for the
	prelude node
	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Move
	language prelude builtins to the prelude context
	* resolve/rust-name-resolution-context.cc: Add code for handling
	the prelude corner case
	* resolve/rust-rib.h: Add a special Prelude rib type

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3315-1.rs: Add test for module with same name
	as builtin
	* rust/compile/issue-3315-2.rs: Test with utilization of i32
	type
	* rust/compile/nr2/exclude: issue-3315-2.rs Does not work with
	NR2.0

Signed-off-by: Liam Naddell <liamnprg@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

3 participants