diff mbox series

[095/125] gccrs: Prevent getting immutable context with classic nr

Message ID 20240801145809.366388-97-arthur.cohen@embecosm.com
State New
Headers show
Series [001/125] Rust: Make 'tree'-level 'MAIN_NAME_P' work | expand

Commit Message

Arthur Cohen Aug. 1, 2024, 2:57 p.m. UTC
From: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Immutable name resolution context is not initialized when the classic
name resolution is in use. It can therefore not be used, the getter would
error out.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_root_path):
	Only get immutable name resolution context when name resolution 2.0 is
	used.
	* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path):
	Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
---
 gcc/rust/typecheck/rust-hir-type-check-path.cc | 14 ++++++++------
 gcc/rust/typecheck/rust-hir-type-check-type.cc | 17 +++++++++--------
 2 files changed, 17 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc b/gcc/rust/typecheck/rust-hir-type-check-path.cc
index b0e52c454e9..dd6ab03a362 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-path.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc
@@ -199,16 +199,18 @@  TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
       bool is_root = *offset == 0;
       NodeId ast_node_id = seg.get_mappings ().get_nodeid ();
 
-      auto nr_ctx
-	= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
-
       // then lookup the reference_node_id
       NodeId ref_node_id = UNKNOWN_NODEID;
 
       if (flag_name_resolution_2_0)
-	// assign the ref_node_id if we've found something
-	nr_ctx.lookup (expr.get_mappings ().get_nodeid ())
-	  .map ([&ref_node_id] (NodeId resolved) { ref_node_id = resolved; });
+	{
+	  auto nr_ctx
+	    = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
+
+	  // assign the ref_node_id if we've found something
+	  nr_ctx.lookup (expr.get_mappings ().get_nodeid ())
+	    .map ([&ref_node_id] (NodeId resolved) { ref_node_id = resolved; });
+	}
       else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
 	resolver->lookup_resolved_type (ast_node_id, &ref_node_id);
 
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index 588e5bce88e..44ebc159b23 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -341,19 +341,20 @@  TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
       bool is_root = *offset == 0;
       NodeId ast_node_id = seg->get_mappings ().get_nodeid ();
 
-      auto nr_ctx
-	= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
-
       // then lookup the reference_node_id
       NodeId ref_node_id = UNKNOWN_NODEID;
 
       // FIXME: HACK: ARTHUR: Remove this
       if (flag_name_resolution_2_0)
-	// assign the ref_node_id if we've found something
-	nr_ctx.lookup (path.get_mappings ().get_nodeid ())
-	  .map ([&ref_node_id, &path] (NodeId resolved) {
-	    ref_node_id = resolved;
-	  });
+	{
+	  auto nr_ctx
+	    = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
+	  // assign the ref_node_id if we've found something
+	  nr_ctx.lookup (path.get_mappings ().get_nodeid ())
+	    .map ([&ref_node_id, &path] (NodeId resolved) {
+	      ref_node_id = resolved;
+	    });
+	}
       else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
 	resolver->lookup_resolved_type (ast_node_id, &ref_node_id);