Skip to content

Conversation

CohenArthur
Copy link
Member

  • nr: Add prelude field to NRCtx, and fill it upon encountering a prelude.
  • forever-stack: Add extra path resolution from a known NodeId.
  • nr: Do prelude resolution for Identifiers

This PR adds name resolution within the prelude marked with #[prelude_import]. This is important for compiling core, and for compiling crates in general - as the compiler is supposed to inject the #[prelude_import] use-declaration if it is not user defined.

The current implementation attempts to resolve within the prelude if regular resolution has failed.

The prelude is collected and stored in the name-resolution context.

gcc/rust/ChangeLog:

	* resolve/rust-early-name-resolver-2.0.cc (Early::finalize_glob_import): Save prelude
	if we find one.
	* resolve/rust-name-resolution-context.h: Add field.
	* resolve/rust-toplevel-name-resolver-2.0.cc (has_prelude_import): New function.
	(TopLevel::visit): Create a prelude glob import if necessary.
	* resolve/rust-toplevel-name-resolver-2.0.h: Allow glob imports to be prelude imports.
gcc/rust/ChangeLog:

	* resolve/rust-forever-stack.h: Add new resolve_path function.
	* resolve/rust-forever-stack.hxx: Implement it.
@CohenArthur CohenArthur force-pushed the std-prelude-resolution branch from 88a2da9 to 8811544 Compare September 12, 2025 15:49
gcc/rust/ChangeLog:

	* resolve/rust-forever-stack.h: New function.
	* resolve/rust-forever-stack.hxx: Implement it.
	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Call it if the prelude exists
	and we have an unresolved Identifier Call it if the prelude exists and we have
	an unresolved Identifier.
@P-E-P P-E-P requested review from powerboat9 and P-E-P September 17, 2025 22:02
{
rust_assert (!segments.empty ());

std::reference_wrapper<Node> starting_point = cursor ();
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 probably just add a new variant to ResolutionMode, instead of adding a user-provided starting_point in addition to mode

Copy link
Collaborator

Choose a reason for hiding this comment

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

ForeverStack could probably fetch the node id associated with the language prelude from NameResolutionContext -- might be kind of tricky to get that working, though. You could also just have each ForeverStack keep a std::reference_wrapper to the currently set prelude Node.

case Namespace::Macros:
return macros.resolve_path (segments, mode,
insert_segment_resolution,
collect_errors, *prelude);
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 probably use a temporary vector instead of collect_errors for the prelude resolution -- if prelude resolution does emit errors and fail, only the errors produced during the normal resolution attempt should be collected.

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

Successfully merging this pull request may close these issues.

2 participants