From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by sourceware.org (Postfix) with ESMTPS id 944CB3860757 for ; Thu, 1 Aug 2024 15:00:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 944CB3860757 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 944CB3860757 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::530 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524465; cv=none; b=lh/7NLoUpdtggULN4PENLwDav8KfxGuh1OC/NmmQBcY/DIxZd1cHCyEp/bI2PMgMFw4fxcVXfWWykAyBDBl/Pi/Xq6x4xXaRbw9AgeuMSES2+as23ns+liJfeVgc8IUf9KF1kU5Lf0y7yPaEGVKnXANRz3Q9PuskhOC+FQltCKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524465; c=relaxed/simple; bh=FSiqVNDJTd42lccRSjJN8evm5MEgwZXnf/NtM4+RKdY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QSdmHY1eI8Ee0F/3W5av8Q69bN59woQQIF7d0mFAHeEK5z7ArWqHnnixjyt5j9b4FvhoR9gSyWLehNx2uCHDy/DMSrM90UbDCUcX3Fjz+jolsb0e4NFERdYkwJfxCiok1seqzf3ntC1eEdf4wmwkX2qCiyJFN7lOzQN6s+0ssRs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-5b214865fecso6006254a12.1 for ; Thu, 01 Aug 2024 08:00:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524412; x=1723129212; 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=uT/wGttbSLDE0qhjTJ3h5tsQBcuguyGU3kRDc/noIa8=; b=X0o6AMgXcMGswr3vD1VLuXl5qDTp5H6WSEociGCoQ9ow7CFxf2TuXI70gFuMKq48p8 wlc9r5O/cE88GMuPPdU24JiC2uIi1HrOyw/cF/AxT9KgykJq0iDf60O3JvftZ0/jbGZj l9Z3ftRP9pN1LD4CMLPXjUs64SXOQ79nFeu8DA0QoXvFnGqFiA7I/6Amq8eQ7CCZfI4j EO1QmCC6Flc59Yav9/MOAKNILflRfxtFVNiiftR6rduTCUv5RhSAAa6cQfeLJFicS7Tz S6MQ7vDsAMNf4BrYooQMBpEvytmyt68yBU2gqhrSwOx/Pjdb3lB71n1t3OUuvHCykRf2 avEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524412; x=1723129212; 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=uT/wGttbSLDE0qhjTJ3h5tsQBcuguyGU3kRDc/noIa8=; b=b6OjbbpZBZomG58jBSYrX8Psky2twNP4SETD6VyX4uSQVIHKHq0UdadlbQ5VLUAYCe NzkYsqOwUhS9ptD3sRro0/tmIZhtN636gVkz8/ma0qRhgjnuIFT8MO1zkAPXVoSr4Pqn G84f6CyNF///671Gs+ghaU9d/VY/TBx3ZdLEMa2DALGzJlXQnVaEBXkCHV7XRgZADnsR 9HOByqnuVg44SSETcoED1PevRP93yxtgtgZ2YaCYeE6MEjGBa1Yf/Y4+T9Pfsl0lGN2q 0jDMfQAmJjkG6Kpl8AgbP53tzvu/cgixwqAqoWUXK34u3F4PeHZhD1Q/zQlohgERRWkH Remw== X-Gm-Message-State: AOJu0Yxs8WskfKF28mX4qMoIIYQu8CpAJDb/uzzNsGrGeZk4/trXD0IX sOJxQ7kIxW1G+6LDdSnBybmRkM012PPNxUl2o5DOCHS/FHwYQ7WgO+dRnPtYH6DaPCI03p7tFNw CV3zP X-Google-Smtp-Source: AGHT+IGXKvDSGQVapuBPCSqNtPe62NxirsXiKOOPkGvRCv9hh5Kr0mBO4JfcA5EGahOkIJT7VUN7Yw== X-Received: by 2002:a50:ee8d:0:b0:5af:6f52:c13d with SMTP id 4fb4d7f45d1cf-5b7f540af84mr435790a12.29.1722524411872; Thu, 01 Aug 2024 08:00:11 -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.08.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 08:00:11 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Pierre-Emmanuel Patry Subject: [PATCH 098/125] gccrs: Fix use rebind name resolution. Date: Thu, 1 Aug 2024 16:57:34 +0200 Message-ID: <20240801145809.366388-100-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 Content-Transfer-Encoding: 8bit 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 List-Id: From: Pierre-Emmanuel Patry Name resolution for rebind were missing. gcc/rust/ChangeLog: * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::handle_use_glob): Change function prototype to use a reference instead. (TopLevel::handle_use_dec): Likewise. (TopLevel::handle_rebind): Add name resolution on rebind use declarations. (flatten_rebind): Change prototype to accept a pair of path/alias. (flatten_list): Adapt call to flatten_rebind. (flatten): Adapt call to flatten_rebind. (flatten_glob): Remove unused part. (TopLevel::visit): Add rebind resolution. * resolve/rust-toplevel-name-resolver-2.0.h: Adapt function prototypes. Signed-off-by: Pierre-Emmanuel Patry --- .../rust-toplevel-name-resolver-2.0.cc | 194 +++++++++++++----- .../resolve/rust-toplevel-name-resolver-2.0.h | 5 +- 2 files changed, 151 insertions(+), 48 deletions(-) diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc index e6da8db850c..4593c67c5d3 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc @@ -17,6 +17,7 @@ // . #include "rust-toplevel-name-resolver-2.0.h" +#include "input.h" #include "optional.h" #include "rust-ast-full.h" #include "rust-hir-map.h" @@ -434,7 +435,7 @@ TopLevel::visit (AST::ConstantItem &const_item) } bool -TopLevel::handle_use_glob (AST::SimplePath glob) +TopLevel::handle_use_glob (AST::SimplePath &glob) { auto resolved = ctx.types.resolve_path (glob.get_segments ()); if (!resolved.has_value ()) @@ -453,7 +454,7 @@ TopLevel::handle_use_glob (AST::SimplePath glob) } bool -TopLevel::handle_use_dec (AST::SimplePath path) +TopLevel::handle_use_dec (AST::SimplePath &path) { auto locus = path.get_final_segment ().get_locus (); auto declared_name = path.get_final_segment ().as_string (); @@ -508,6 +509,97 @@ TopLevel::handle_use_dec (AST::SimplePath path) }); }; + resolve_and_insert (Namespace::Values, path); + resolve_and_insert (Namespace::Types, path); + resolve_and_insert (Namespace::Macros, path); + + return found; +} + +bool +TopLevel::handle_rebind (std::pair &rebind) +{ + auto &path = rebind.first; + + location_t locus = UNKNOWN_LOCATION; + std::string declared_name; + + switch (rebind.second.get_new_bind_type ()) + { + case AST::UseTreeRebind::NewBindType::IDENTIFIER: + declared_name = rebind.second.get_identifier ().as_string (); + locus = rebind.second.get_identifier ().get_locus (); + break; + case AST::UseTreeRebind::NewBindType::NONE: + declared_name = path.get_final_segment ().as_string (); + locus = path.get_final_segment ().get_locus (); + break; + case AST::UseTreeRebind::NewBindType::WILDCARD: + rust_unreachable (); + break; + } + + // in what namespace do we perform path resolution? All + // of them? see which one matches? Error out on + // ambiguities? so, apparently, for each one that + // matches, add it to the proper namespace + // :( + auto found = false; + + auto resolve_and_insert = [this, &found, &declared_name, + locus] (Namespace ns, + const AST::SimplePath &path) { + tl::optional resolved = tl::nullopt; + tl::optional resolved_bind = tl::nullopt; + + std::vector declaration_v + = {AST::SimplePathSegment (declared_name, locus)}; + // FIXME: resolve_path needs to return an `expected` so + // that we can improve it with hints or location or w/ever. and maybe + // only emit it the first time. + switch (ns) + { + case Namespace::Values: + resolved = ctx.values.resolve_path (path.get_segments ()); + resolved_bind = ctx.values.resolve_path (declaration_v); + break; + case Namespace::Types: + resolved = ctx.types.resolve_path (path.get_segments ()); + resolved_bind = ctx.types.resolve_path (declaration_v); + break; + case Namespace::Macros: + resolved = ctx.macros.resolve_path (path.get_segments ()); + resolved_bind = ctx.macros.resolve_path (declaration_v); + break; + case Namespace::Labels: + // TODO: Is that okay? + rust_unreachable (); + } + + resolved.map ([this, &found, &declared_name, locus, ns, path, + &resolved_bind] (Rib::Definition def) { + found = true; + + insert_or_error_out (declared_name, locus, def.get_node_id (), ns); + if (resolved_bind.has_value ()) + { + auto bind_def = resolved_bind.value (); + // what do we do with the id? + auto result = node_forwarding.find (bind_def.get_node_id ()); + if (result != node_forwarding.cend () + && result->second != path.get_node_id ()) + rust_error_at (path.get_locus (), "%qs defined multiple times", + declared_name.c_str ()); + } + else + { + // No previous thing has inserted this into our scope + node_forwarding.insert ({def.get_node_id (), path.get_node_id ()}); + } + return def.get_node_id (); + }); + }; + // do this for all namespaces (even Labels?) resolve_and_insert (Namespace::Values, path); @@ -520,31 +612,38 @@ TopLevel::handle_use_dec (AST::SimplePath path) } static void -flatten_rebind (const AST::UseTreeRebind &glob, - std::vector &paths); +flatten_rebind ( + const AST::UseTreeRebind &glob, + std::vector> &rebind_paths); + static void -flatten_list (const AST::UseTreeList &glob, std::vector &paths, - std::vector &glob_paths, - NameResolutionContext &ctx); +flatten_list ( + const AST::UseTreeList &glob, std::vector &paths, + std::vector &glob_paths, + std::vector> &rebind_paths, + NameResolutionContext &ctx); static void flatten_glob (const AST::UseTreeGlob &glob, std::vector &glob_paths, NameResolutionContext &ctx); static void -flatten (const AST::UseTree *tree, std::vector &paths, - std::vector &glob_paths, NameResolutionContext &ctx) +flatten ( + const AST::UseTree *tree, std::vector &paths, + std::vector &glob_paths, + std::vector> &rebind_paths, + NameResolutionContext &ctx) { switch (tree->get_kind ()) { case AST::UseTree::Rebind: { auto rebind = static_cast (tree); - flatten_rebind (*rebind, paths); + flatten_rebind (*rebind, rebind_paths); break; } case AST::UseTree::List: { auto list = static_cast (tree); - flatten_list (*list, paths, glob_paths, ctx); + flatten_list (*list, paths, glob_paths, rebind_paths, ctx); break; } case AST::UseTree::Glob: { @@ -557,27 +656,11 @@ flatten (const AST::UseTree *tree, std::vector &paths, } static void -flatten_rebind (const AST::UseTreeRebind &rebind, - std::vector &paths) +flatten_rebind ( + const AST::UseTreeRebind &rebind, + std::vector> &rebind_paths) { - auto path = rebind.get_path (); - - // FIXME: Do we want to emplace the rebind here as well? - if (rebind.has_identifier ()) - { - auto rebind_path = path; - auto new_seg = rebind.get_identifier (); - - // Add the identifier as a new path - rebind_path.get_segments ().back () - = AST::SimplePathSegment (new_seg.as_string (), UNDEF_LOCATION); - - paths.emplace_back (rebind_path); - } - else - { - paths.emplace_back (path); - } + rebind_paths.emplace_back (rebind.get_path (), rebind); } /** Prefix a list of subpath @@ -599,9 +682,27 @@ prefix_subpaths (AST::SimplePath prefix, std::vector subs, } static void -flatten_list (const AST::UseTreeList &list, std::vector &paths, - std::vector &glob_paths, - NameResolutionContext &ctx) +prefix_rebinds ( + AST::SimplePath prefix, + std::vector> subs, + std::vector> &results) +{ + for (auto &sub : subs) + { + auto new_path = prefix; + std::copy (sub.first.get_segments ().begin (), + sub.first.get_segments ().end (), + std::back_inserter (new_path.get_segments ())); + results.emplace_back (std::make_pair (new_path, sub.second)); + } +} + +static void +flatten_list ( + const AST::UseTreeList &list, std::vector &paths, + std::vector &glob_paths, + std::vector> &rebind_paths, + NameResolutionContext &ctx) { auto prefix = AST::SimplePath::create_empty (); if (list.has_path ()) @@ -611,10 +712,13 @@ flatten_list (const AST::UseTreeList &list, std::vector &paths, { auto sub_paths = std::vector (); auto sub_globs = std::vector (); - flatten (tree.get (), sub_paths, sub_globs, ctx); + auto sub_rebinds + = std::vector> (); + flatten (tree.get (), sub_paths, sub_globs, sub_rebinds, ctx); prefix_subpaths (prefix, sub_paths, paths); prefix_subpaths (prefix, sub_globs, glob_paths); + prefix_rebinds (prefix, sub_rebinds, rebind_paths); } } @@ -624,16 +728,6 @@ flatten_glob (const AST::UseTreeGlob &glob, std::vector &paths, { if (glob.has_path ()) paths.emplace_back (glob.get_path ()); - - // (PE): Get path rib - auto rib = ctx.values.resolve_path (glob.get_path ().get_segments ()) - .and_then ([&] (Rib::Definition def) { - return ctx.values.to_rib (def.get_node_id ()); - }); - if (rib.has_value ()) - { - auto value = rib.value ().get_values (); - } } void @@ -641,13 +735,15 @@ TopLevel::visit (AST::UseDeclaration &use) { auto paths = std::vector (); auto glob_path = std::vector (); + auto rebind_path + = std::vector> (); // FIXME: How do we handle `use foo::{self}` imports? Some beforehand cleanup? // How do we handle module imports in general? Should they get added to all // namespaces? const auto &tree = use.get_tree (); - flatten (tree.get (), paths, glob_path, this->ctx); + flatten (tree.get (), paths, glob_path, rebind_path, this->ctx); for (auto &path : paths) if (!handle_use_dec (path)) @@ -658,6 +754,12 @@ TopLevel::visit (AST::UseDeclaration &use) if (!handle_use_glob (glob)) rust_error_at (glob.get_final_segment ().get_locus (), ErrorCode::E0433, "unresolved import %qs", glob.as_string ().c_str ()); + + for (auto &rebind : rebind_path) + if (!handle_rebind (rebind)) + rust_error_at (rebind.first.get_final_segment ().get_locus (), + ErrorCode::E0433, "unresolved import %qs", + rebind.first.as_string ().c_str ()); } } // namespace Resolver2_0 diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h index e226c79dfbc..fb16866aeb1 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h @@ -108,8 +108,9 @@ private: // Call this on all the paths of a UseDec - so each flattened path in a // UseTreeList for example // FIXME: Should that return `found`? - bool handle_use_dec (AST::SimplePath path); - bool handle_use_glob (AST::SimplePath glob); + bool handle_use_dec (AST::SimplePath &path); + bool handle_use_glob (AST::SimplePath &glob); + bool handle_rebind (std::pair &pair); void visit (AST::UseDeclaration &use) override; }; -- 2.45.2