From patchwork Thu Aug 1 14:57:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 95006 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 56F43386100C for ; Thu, 1 Aug 2024 15:21:54 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id BBDBB3860C33 for ; Thu, 1 Aug 2024 14:59:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BBDBB3860C33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BBDBB3860C33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::531 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524406; cv=none; b=qcv3GRLiigEpQ3+Po7FlUfrJBxUbu+HpQWeZLw7RoEhbPowp/iZkVfttwMw5pgkxd5zp4CFHn9R7oFXl4TSk+VCtrWYy8M5iYq0YSZaRp0QVHXWGmMGr113Y9aZ4PsF6yBgBNGk+tLzayTB07LskqaoGubmVs8DPEJzL0CiuZho= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524406; c=relaxed/simple; bh=NeJRC+6HE5fqzkQUKJ+97ta+rAsTXIyBRqcxWlXZDMc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qKw8qIPJO8ZmqDQiD9r3p3lo+y1R4tHthKxlwTWI3GlqvcNrE5b9uvDADEo5hy6Ushaupsin0gDCuyzA/KSfObuEKJDGYuYMjPHdDPMXZWTIOpdV6SwmqrpduIgCUeix3fBA/kVOfjKr15NKhkknviesBoiuWASCNsDADNBUHH4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-5afa207b8bfso7403942a12.0 for ; Thu, 01 Aug 2024 07:59:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524371; x=1723129171; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=53xQQ77GSbhk1By6YlTPXkOcRMBwUx8bxC61c9KlWqY=; b=O7PLWjEo4euWMJUJ2F5mYZiQovZiHWdOJ2XZ3sWbWMLQP4w2syX7MZksxNMNJBnjb9 IirTxf7BRLWWGX034Z/KlbQbSbPPuGElZalLWetnVPAbhglwl8EHLJYFgOgSlp6girSn akx9KG0Qgll+OG0HOBmyRuOP44ApQ+8S7tg82qm5EOZP0hLn5ui4sdpjCOtRy4coEj1Y TkKpk5DAxo8DnXNW0aWRQF499rAeKdBdNnHCcyp6n91vf1BpQAg4yc3AvtgJ8/r/dkDX 9bVSy3iDGiBix/zf+aIIgdL2pPuAhR3NFJTAPAYYhW5Q8eRMQuxd8oLusGN6qYBYSkXc 58Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524371; x=1723129171; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=53xQQ77GSbhk1By6YlTPXkOcRMBwUx8bxC61c9KlWqY=; b=Q+yqrg9Iq6BrMAzXyjj95FxaO8LO7sWSGdXoitkQILqo5Ihx/W9wEybGD9ul6ofUH/ ao2gM+6uBhE9dfksYj/r5GhZqTkQABYfOsuB0Kso/kVoHV3DdLkL9kngu0OqDIB83Kzp 3j2Js/T9GR901WM3V/FOeo27iPpdAd34BoXbPEqLP8nbHUrFgWFMbVlPxdGVLbHnb0Jg pPsMQXrgJ0h51Tof6prwEC61xLGXNRXLFAdjIHTelp583g/mmKMLF43zAmQ8A4OOZit5 HgOOTheKhpzbNOm03tj3jB/6RuxYqUpAijxPcGMyHKLSbLd4mNA+Q3xnMXotLQoEP1zU 65CA== X-Gm-Message-State: AOJu0YwLF0h2K86GaR4jFZ1a+eQyKAFQHtVkwdEGwFfboV4lAoHMlReS WLB2GuJ832yFR9fVvAskmAWiloGzGNO/VR+Qf7sKntXmv3zBIqoR08qduHSzdkUPNNwNN0Cp6qP FBFdF X-Google-Smtp-Source: AGHT+IHjacZqggxQ5owqQj8MUoCKIUtX+eRDRojWZykHjDxlz99RGZA3OZ7L/+84xDKZ/V255y2xNg== X-Received: by 2002:a05:6402:2d5:b0:59e:6b89:fdad with SMTP id 4fb4d7f45d1cf-5b7f340b63emr432517a12.3.1722524371125; Thu, 01 Aug 2024 07:59:31 -0700 (PDT) Received: from platypus.lan ([2a04:cec2:9:dc84:3622:6733:ff49:ee91]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590592sm10252456a12.25.2024.08.01.07.59.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:59:30 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [PATCH 064/125] gccrs: typecheck: Start using nr2.0 properly Date: Thu, 1 Aug 2024 16:57:00 +0200 Message-ID: <20240801145809.366388-66-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801145809.366388-2-arthur.cohen@embecosm.com> References: <20240801145809.366388-2-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Fetch the ImmutableNrCtx in order to access name resolver during typechecking. gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Start fetching name resolution information in the typechecker. * typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): Likewise. * typecheck/rust-hir-type-check-path.cc: Use nr 2.0. --- .../typecheck/rust-hir-type-check-item.cc | 91 +++++++++++++++---- .../typecheck/rust-hir-type-check-path.cc | 16 +++- .../typecheck/rust-hir-type-check-type.cc | 24 ++++- 3 files changed, 105 insertions(+), 26 deletions(-) diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index 4bbd28021a0..4ab946e1c2a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -17,12 +17,17 @@ // . #include "rust-hir-type-check-item.h" +#include "rust-canonical-path.h" +#include "rust-diagnostics.h" #include "rust-hir-type-check-enumitem.h" #include "rust-hir-type-check-implitem.h" #include "rust-hir-type-check-type.h" #include "rust-hir-type-check-expr.h" #include "rust-hir-type-check-pattern.h" #include "rust-hir-trait-resolve.h" +#include "rust-identifier.h" +#include "rust-session-manager.h" +#include "rust-immutable-name-resolution-context.h" #include "rust-substitution-mapper.h" #include "rust-type-util.h" #include "rust-tyty-variance-analysis.h" @@ -185,11 +190,30 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl) } // get the path - const CanonicalPath *canonical_path = nullptr; - bool ok = mappings->lookup_canonical_path ( - struct_decl.get_mappings ().get_nodeid (), &canonical_path); - rust_assert (ok); - RustIdent ident{*canonical_path, struct_decl.get_locus ()}; + + auto path = CanonicalPath::create_empty (); + + // FIXME: HACK: ARTHUR: Disgusting + if (flag_name_resolution_2_0) + { + auto nr_ctx + = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); + auto canonical_path = nr_ctx.values.to_canonical_path ( + struct_decl.get_mappings ().get_nodeid ()); + + path = canonical_path.value (); + } + else + { + const CanonicalPath *canonical_path = nullptr; + bool ok = mappings->lookup_canonical_path ( + struct_decl.get_mappings ().get_nodeid (), &canonical_path); + rust_assert (ok); + + path = *canonical_path; + } + + RustIdent ident{path, struct_decl.get_locus ()}; // its a single variant ADT std::vector variants; @@ -248,12 +272,29 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl) context->insert_type (field.get_mappings (), ty_field->get_field_type ()); } - // get the path - const CanonicalPath *canonical_path = nullptr; - bool ok = mappings->lookup_canonical_path ( - struct_decl.get_mappings ().get_nodeid (), &canonical_path); - rust_assert (ok); - RustIdent ident{*canonical_path, struct_decl.get_locus ()}; + auto path = CanonicalPath::create_empty (); + + // FIXME: HACK: ARTHUR: Disgusting + if (flag_name_resolution_2_0) + { + auto nr_ctx + = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); + auto canonical_path = nr_ctx.values.to_canonical_path ( + struct_decl.get_mappings ().get_nodeid ()); + + path = canonical_path.value (); + } + else + { + const CanonicalPath *canonical_path = nullptr; + bool ok = mappings->lookup_canonical_path ( + struct_decl.get_mappings ().get_nodeid (), &canonical_path); + rust_assert (ok); + + path = *canonical_path; + } + + RustIdent ident{path, struct_decl.get_locus ()}; // its a single variant ADT std::vector variants; @@ -510,13 +551,29 @@ TypeCheckItem::visit (HIR::Function &function) TypeCheckPattern::Resolve (param.get_param_name ().get (), param_tyty); } - const CanonicalPath *canonical_path = nullptr; - bool ok - = mappings->lookup_canonical_path (function.get_mappings ().get_nodeid (), - &canonical_path); - rust_assert (ok); + auto path = CanonicalPath::create_empty (); + + // FIXME: HACK: ARTHUR: Disgusting + if (flag_name_resolution_2_0) + { + auto nr_ctx + = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); + auto canonical_path = nr_ctx.values.to_canonical_path ( + function.get_mappings ().get_nodeid ()); + + path = canonical_path.value (); + } + else + { + const CanonicalPath *canonical_path = nullptr; + bool ok = mappings->lookup_canonical_path ( + function.get_mappings ().get_nodeid (), &canonical_path); + rust_assert (ok); + + path = *canonical_path; + } - RustIdent ident{*canonical_path, function.get_locus ()}; + RustIdent ident{path, function.get_locus ()}; auto fn_type = new TyTy::FnType ( function.get_mappings ().get_hirid (), diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc b/gcc/rust/typecheck/rust-hir-type-check-path.cc index ad31fb74a80..cdb506dacbe 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-path.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc @@ -24,6 +24,8 @@ #include "rust-hir-path-probe.h" #include "rust-type-util.h" #include "rust-hir-type-bounds.h" +#include "rust-session-manager.h" +#include "rust-immutable-name-resolution-context.h" namespace Rust { namespace Resolver { @@ -197,12 +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 (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id)) - { - resolver->lookup_resolved_type (ast_node_id, &ref_node_id); - } + + 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; }); + else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id)) + resolver->lookup_resolved_type (ast_node_id, &ref_node_id); // ref_node_id is the NodeId that the segments refers to. if (ref_node_id == UNKNOWN_NODEID) diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index a132b386a96..588e5bce88e 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -17,10 +17,13 @@ // . #include "rust-hir-type-check-type.h" +#include "options.h" #include "rust-hir-trait-resolve.h" #include "rust-hir-type-check-expr.h" #include "rust-hir-path-probe.h" #include "rust-hir-type-bounds.h" +#include "rust-immutable-name-resolution-context.h" +#include "rust-mapping-common.h" #include "rust-substitution-mapper.h" #include "rust-type-util.h" @@ -129,6 +132,8 @@ TypeCheckType::visit (HIR::TupleType &tuple) void TypeCheckType::visit (HIR::TypePath &path) { + rust_debug ("{ARTHUR}: Path visited: %s", path.as_string ().c_str ()); + // this can happen so we need to look up the root then resolve the // remaining segments if possible size_t offset = 0; @@ -336,12 +341,21 @@ 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; - if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id)) - { - resolver->lookup_resolved_type (ast_node_id, &ref_node_id); - } + + // 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; + }); + else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id)) + resolver->lookup_resolved_type (ast_node_id, &ref_node_id); // ref_node_id is the NodeId that the segments refers to. if (ref_node_id == UNKNOWN_NODEID) @@ -349,7 +363,7 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset, if (is_root) { rust_error_at (seg->get_locus (), - "unknown reference for resolved name: %<%s%>", + "unknown reference for resolved name: %qs", seg->get_ident_segment ().as_string ().c_str ()); return new TyTy::ErrorType (path.get_mappings ().get_hirid ()); }