From patchwork Thu Aug 1 14:57:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 95008 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 E69E8386075D for ; Thu, 1 Aug 2024 15:21:56 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id C58273861002 for ; Thu, 1 Aug 2024 14:59:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C58273861002 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 C58273861002 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524415; cv=none; b=V8Ufac0Pr3ZhL1dMJ3SmdUYyRvRvCmZWlWNqO+O2hFoVaZTEXyxzHCCDzGXhJGG/vpn6WZOMLJiLrgoXHpj/PmScAzCMy9RNFANx6xbjYrAslr6VLlxuEBpEH03aQZlkQdMWLeekrAAi+MBOpP9T3d5/FOvm610eEMTvOq/QpoM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524415; c=relaxed/simple; bh=ebEops2BVvpWP2TcPoHhkJdRmlX2EnjR0hFN2pJXtlE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Ei4xiB1Zz0vFQUf8ij9Op1QPczs00CQJPV5JHGADNH2lXCHRYo5SGJr66dWvk+gQ6STvkZLppJBQmbHLvp1WeZMVUcn8RLPwtZyZ51btUFJmNBV9xqdNgcylxxdklbXGWekr5JGoc1+HBfRZIqdElJYVk8sSMC/f9O4y6bbBK90= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-52fc4388a64so11844927e87.1 for ; Thu, 01 Aug 2024 07:59:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524375; x=1723129175; 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=jXzEbKsWJbaWW5tPuTwl0pL+QcJbSQ8P5rrvHR0dk2Y=; b=Nz9zcZmYxfTZYy66z8Sw5HbKrRQcjzod0W9eUnj3Af0uQXALJ+nL4HYwiimvy/g3bK rP43PWAzXzx1yFWGL+NqS04Pck3AxzZsVtb1MkBkoebV7iuVLakeNFDKYV4tcaGLo2nf OzPH5Fpwv0rc1iD5CmlEX1VedKVySCfG3VSOFY0wv9DB8QWHnrwg2FyWqiDXedNYW9vZ nPYiqP9lWEXt/9DpHK6YSe7wev1nyc+rndLaYyIsUPV54bjE8+LDhnXAoTrNI00fHWJe 9dCn2yM22RwKF+s+AQ+CBPPSqwiGOKodVaPmA2kqfFMDoa4qKQqbVeXq1MPcwdsaA5PX JhyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524375; x=1723129175; 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=jXzEbKsWJbaWW5tPuTwl0pL+QcJbSQ8P5rrvHR0dk2Y=; b=f0AYi4HQm0pdMOJyVv5d9LEtZ13gH5OahEctE2xiYb1VIT+C6Cu1veD/cpLDQR0bGb WYc6/8Ngu4Z16ZFPos/AX1H/QWVT752rCQ1nI6fxGShcS+VAL2tWIO+KX3ANRPbOdU8O XEUnQ5w6CA79IsZ7Pi4d2s10S9ycMBazqeJL3VJ3+3Hlk7NKbKvGLZhjdvNYaPjT0+Su FWoUc2QNbT9cKwobJeCYF9sAQI/Ql2LfmXfyrnpjLw0NKH+0WkRmTnBfAwpaqdNvJK97 1d6rWpd+RluugyysxI+wyjpCrQJjQ/TUAK4EjrS3KsDl9noJ4YHauJW5QjxwxAQ0TTCn ZhZg== X-Gm-Message-State: AOJu0YxqnPsZ+c2BBF4MCU/VJbG7trpT1XEdXZi34OJFNX6bwHHoGvit 1BDFKabodrcI2dgRa6PVbKvqeN5FtNzNQl2DtVHPdTHOgJGhpchTCk1q3BQMGTLt749R6ZV6/9p NZt4w X-Google-Smtp-Source: AGHT+IFkLe9uUEzm7JBMlzoVuC7QtYG09zkQwYiTxQ8eMeqgnxjDyda+VsN5/QVstAJHl2xezojT0Q== X-Received: by 2002:a05:6512:3b8c:b0:52f:44b:7d42 with SMTP id 2adb3069b0e04-530bb3b4340mr106523e87.58.1722524374862; Thu, 01 Aug 2024 07:59:34 -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.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:59:34 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Pierre-Emmanuel Patry Subject: [PATCH 069/125] gccrs: Emit error on identical use declarations Date: Thu, 1 Aug 2024 16:57:05 +0200 Message-ID: <20240801145809.366388-71-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, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham 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 From: Pierre-Emmanuel Patry The compiler did not emit any warning when a same target was declared from different sources. gcc/rust/ChangeLog: * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::handle_use_dec): Use the new dict to track down already resolved use declarations. * resolve/rust-toplevel-name-resolver-2.0.h: Add new dict to store previous use declarations. Signed-off-by: Pierre-Emmanuel Patry --- .../rust-toplevel-name-resolver-2.0.cc | 76 ++++++++++--------- .../resolve/rust-toplevel-name-resolver-2.0.h | 4 + 2 files changed, 46 insertions(+), 34 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 407892bb7bb..94cc3cb62d9 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc @@ -319,40 +319,48 @@ TopLevel::handle_use_dec (AST::SimplePath path) auto found = false; - auto resolve_and_insert = [this, &found, &declared_name, - locus] (Namespace ns, - const AST::SimplePath &path) { - tl::optional resolved = tl::nullopt; - - // 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 ()); - break; - case Namespace::Types: - resolved = ctx.types.resolve_path (path.get_segments ()); - break; - case Namespace::Macros: - resolved = ctx.macros.resolve_path (path.get_segments ()); - break; - case Namespace::Labels: - // TODO: Is that okay? - rust_unreachable (); - } - - // FIXME: Ugly - (void) resolved.map ([this, &found, &declared_name, locus, ns] (NodeId id) { - found = true; - - // what do we do with the id? - insert_or_error_out (declared_name, locus, id, ns); - - return id; - }); - }; + auto resolve_and_insert + = [this, &found, &declared_name, locus] (Namespace ns, + const AST::SimplePath &path) { + tl::optional resolved = tl::nullopt; + + // 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 ()); + break; + case Namespace::Types: + resolved = ctx.types.resolve_path (path.get_segments ()); + break; + case Namespace::Macros: + resolved = ctx.macros.resolve_path (path.get_segments ()); + break; + case Namespace::Labels: + // TODO: Is that okay? + rust_unreachable (); + } + + // FIXME: Ugly + (void) resolved.map ( + [this, &found, &declared_name, locus, ns, path] (NodeId id) { + found = true; + + // what do we do with the id? + insert_or_error_out (declared_name, locus, id, ns); + auto result = node_forwarding.find (id); + if (result != node_forwarding.cend () + && result->second != path.get_node_id ()) + rust_error_at (path.get_locus (), "%<%s%> defined multiple times", + declared_name.c_str ()); + else // No previous thing has inserted this into our scope + node_forwarding.insert ({id, path.get_node_id ()}); + + return id; + }); + }; // do this for all namespaces (even Labels?) 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 ac11f310370..0a766bab259 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h @@ -60,6 +60,10 @@ private: // FIXME: Do we move these to our mappings? std::unordered_map node_locations; + // Store node forwarding for use declaration, the link between a + // "new" local name and its definition. + std::unordered_map node_forwarding; + void visit (AST::Module &module) override; void visit (AST::MacroRulesDefinition ¯o) override; void visit (AST::Function &function) override;