{"id":2223288,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2223288/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/20260414231905.191063-22-arthur.cohen@embecosm.com/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/1.1/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null},"msgid":"<20260414231905.191063-22-arthur.cohen@embecosm.com>","date":"2026-04-14T23:18:43","name":"[COMMITTED,22/43] gccrs: Move builtin attribute checking to its own pass","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"b00d88da20ddfc835590484615275cbc48fdcf3c","submitter":{"id":83476,"url":"http://patchwork.ozlabs.org/api/1.1/people/83476/?format=json","name":"Arthur Cohen","email":"arthur.cohen@embecosm.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/20260414231905.191063-22-arthur.cohen@embecosm.com/mbox/","series":[{"id":499900,"url":"http://patchwork.ozlabs.org/api/1.1/series/499900/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=499900","date":"2026-04-14T23:18:24","name":"[COMMITTED,01/43] gccrs: testsuite:Add a testcase for setup_associated_types","version":1,"mbox":"http://patchwork.ozlabs.org/series/499900/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2223288/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2223288/checks/","tags":{},"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256\n header.s=google header.b=MNcwOvrC;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256\n header.s=google header.b=MNcwOvrC","sourceware.org;\n dmarc=none (p=none dis=none) header.from=embecosm.com","sourceware.org; spf=pass smtp.mailfrom=embecosm.com","server2.sourceware.org;\n arc=none smtp.remote-ip=209.85.128.66"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fwL2L3VTzz1yHM\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 15 Apr 2026 09:24:10 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 79F574BA2E23\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 14 Apr 2026 23:24:08 +0000 (GMT)","from mail-wm1-f66.google.com (mail-wm1-f66.google.com\n [209.85.128.66])\n by sourceware.org (Postfix) with ESMTPS id E49DD4BA2E26\n for <gcc-patches@gcc.gnu.org>; Tue, 14 Apr 2026 23:14:41 +0000 (GMT)","by mail-wm1-f66.google.com with SMTP id\n 5b1f17b1804b1-488ba6366a7so75542165e9.0\n for <gcc-patches@gcc.gnu.org>; Tue, 14 Apr 2026 16:14:41 -0700 (PDT)","from platypus.localdomain (176-147-231-59.abo.bbox.fr.\n [176.147.231.59]) by smtp.gmail.com with ESMTPSA id\n 5b1f17b1804b1-488f1dd8806sm3325945e9.3.2026.04.14.16.14.38\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Tue, 14 Apr 2026 16:14:39 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 79F574BA2E23","OpenDKIM Filter v2.11.0 sourceware.org E49DD4BA2E26"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org E49DD4BA2E26","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org E49DD4BA2E26","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776208482; cv=none;\n b=jBpNMW9AfixY0wRKu0VLi+vTmvCLZhYJk1GwJj84/tvPStvhJ8QyVmdg1+TpngwxXePLzG5rTzoD2PhPw0Wc23tCgqCJ2KWQMml0lcNobVnhzXbPSzMRpjHVW6cma8O2PEcOSiV+WNLFfylYPgpxFknr2roI7NuKLEeJ4I9vQuw=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776208482; c=relaxed/simple;\n bh=swqQLf6rmmWB5owCCkF0XfF2WiqXbHUSDDJU84pQs5M=;\n h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version;\n b=RBSDldTd2iosUmTHvNqy1Y1amHVQ+LmhOqy4cPHUuwHnMshl/Li2nC1R33ZUcgonXcV6v8zc3rDLX0kJxn3UVJ7HbMrFX2Pe9TE5JsRwF53Nd8bH5sIOfOqcx8Dd4L98zkgZS9p0v9Ag1RY0YS+bLptUwsTsBuTZfsuUcDFYIms=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=embecosm.com; s=google; t=1776208481; x=1776813281; darn=gcc.gnu.org;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:from:to:cc:subject:date\n :message-id:reply-to;\n bh=o/AwiaW9O+c3H1/l8wCH4GRlKnH279dbtrXMabfSeUg=;\n b=MNcwOvrCC1lHYLVbX7vdXNHw+W6sEIKGX9ubbgdkuDvt2wNBdGoHQWUKgdmjjH4J7H\n qh3+6e9zcHIfYppZ2PZclEf2iKupd/mbYH5DjchYZaEttC1+Fn0sj4PXI2w8PGVg4BE4\n ttRvBOEXWdyl7OX5ud+QBXI5ENQ7p3AvJIxBZduU3wQBVvOIYKx4/HGmzJYpIJIfc01K\n nkeKBok9hdaGKiDNkjVgMCvFxMN5pJiZ4jPl+UhOjNtinFLfXZab8gJ+p2uDYqDrSKkG\n 9irK3adKouRgc4ldwf2qp5h8/9BZg/LnvRMGFpn0XzsFYPnzf5eq2Nt8XLIeflQh7DfY\n 9XXA==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1776208481; x=1776813281;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=o/AwiaW9O+c3H1/l8wCH4GRlKnH279dbtrXMabfSeUg=;\n b=bE+leMwCgeYUOgqTh5xcpOzlQ2udMDTSpn3GTF0DQaqf3I/w5ErJ7b1X8e94JzpFW1\n 6MEyiH4WO8WShFGFKdmWJEJJjtFGw2okb6dFAZ1Sx4rbE76ljIKi2yefqyzdPQrkwhBs\n vvNRuVwg2TKybcujkMbFtJcvv0b22WueBkD1bGSMD0e68EGa08l4QZL9gRfRihxYmAvv\n JuXNza5mJmSeeT+uAvFIRZRIhUA3yRr1MP2A7aDhtm31dyADQBhRrpFSf6Je+0Gc270K\n lGhhWZQA/OSIngIgF7JAsXhGBdOF/I2+hfVYJwHiGEREdwGF2jO69/7MQO3Hfr8SXKwh\n G72A==","X-Gm-Message-State":"AOJu0YypS9I4cBANR4/s1pXiyqTkd4Ned3zt4xZKZIAV0P6b0RZZwYTI\n mGRXoLd2tLJ/VwTRmgFUikxm5s+O4NK9XC7I5zb+2FHB/jCb86woBHmrnqpX8a9KlZz7rHgWQUl\n tm8EGwRK1","X-Gm-Gg":"AeBDieuaRoyQNnasj9/t/vKz0dYqtdH9m+5RKGxdSgP2sWSa8+fY8dtj8z5CD4+4EOM\n nMIli7a3hHwVImSATE+2/CW6sEN1f6MkZcKZTfOM2e9135G/LDN27DXgzfxdUIx5H+W7newpgSC\n 4l1xoP9AMQXuExxTzQxXxNK6UsWsA8a9yY+MugwIKOKQxKp72HHAdwxCuOsITjRLUVRSHkdcIq9\n pGjcG2o/AEYTpjYi3xJ/3EIFz5gWU5H2lfaItSLQaB0Acy0r71mUnKJ5z3h5orG26RaNQ2FguWp\n AaaakGHBxg2YBSpVPyGVtemssIswrQuR4sa7J02YwLSqVKyM1pubDSNqLjEkbumiH6yBiEZnxsr\n aL+JtWj471MqZnO+mFPtvZB2OfUSNip9QIPkLYfGFp5KzkOhSGWEN1NMZ85FzUhQAP77OlMHZUX\n /HRyWCT4PfSC3hdm20TrL6X+Ykvr6pbWgdTdEdXyrBkEU2H3SbhVF/Bzivz2WwhrD/5ZiKFjAHi\n BBtSA==","X-Received":"by 2002:a05:600c:2d8c:b0:488:dbcb:ca08 with SMTP id\n 5b1f17b1804b1-488dbcbca57mr140424275e9.30.1776208480378;\n Tue, 14 Apr 2026 16:14:40 -0700 (PDT)","From":"arthur.cohen@embecosm.com","To":"gcc-patches@gcc.gnu.org","Cc":"gcc-rust@gcc.gnu.org,\n Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>","Subject":"[COMMITTED 22/43] gccrs: Move builtin attribute checking to its own\n pass","Date":"Wed, 15 Apr 2026 01:18:43 +0200","Message-ID":"<20260414231905.191063-22-arthur.cohen@embecosm.com>","X-Mailer":"git-send-email 2.50.1","In-Reply-To":"<20260414231905.191063-1-arthur.cohen@embecosm.com>","References":"<20260414231905.191063-1-arthur.cohen@embecosm.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"From: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>\n\nBuiltin attribute checking should be done after macro expansion because\nwe want to support macros within attributes.\n\ngcc/rust/ChangeLog:\n\n\t* Make-lang.in: Add file for builtin attribute checking.\n\t* hir/rust-ast-lower-base.cc (ASTLoweringBase::handle_doc_item_attribute):\n\tRemove error emission and replace it with an assert. The error message\n\twill now be emitted from the builtin attribute checking pass.\n\t* rust-session-manager.cc (Session::compile_crate): Add builtin\n\tattribute checking step after expansion.\n\t* util/rust-attributes.cc (Attributes::is_known): Move down.\n\t(Attributes::extract_string_literal): Likewise.\n\t(Attributes::valid_outer_attribute): Add a function to retrieve outer/\n\tinner status for a given builtin attribute value.\n\t(AttributeChecker::visit): Move some checking to builtin attribute\n\tchecker.\n\t(identify_builtin): Rename function from here ...\n\t(lookup_builtin): ... to here.\n\t(check_doc_alias): Move to builtin attribute checker.\n\t(check_doc_attribute): Likewise.\n\t(check_deprecated_attribute): Likewise.\n\t(check_valid_attribute_for_item): Likewise.\n\t(AttributeChecker::check_inner_attribute): Likewise.\n\t(check_link_section_attribute): Likewise.\n\t(check_export_name_attribute): Likewise.\n\t(check_lint_attribute): Likewise.\n\t(check_no_mangle_function): Likewise.\n\t(is_proc_macro_type): Update function call name.\n\t* util/rust-attributes.h (identify_builtin): Update prototype.\n\t(lookup_builtin): Likewise.\n\t* checks/errors/rust-builtin-attribute-checker.cc: New file.\n\t* checks/errors/rust-builtin-attribute-checker.h: New file.\n\ngcc/testsuite/ChangeLog:\n\n\t* rust/compile/issue-4226.rs: Update text to match error message from\n\tattribute checking pass that was not triggered before.\n\nSigned-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>\n---\n gcc/rust/Make-lang.in                         |   1 +\n .../errors/rust-builtin-attribute-checker.cc  | 602 ++++++++++++++++++\n .../errors/rust-builtin-attribute-checker.h   |  58 ++\n gcc/rust/hir/rust-ast-lower-base.cc           |   8 +-\n gcc/rust/rust-session-manager.cc              |   3 +\n gcc/rust/util/rust-attributes.cc              | 571 ++---------------\n gcc/rust/util/rust-attributes.h               |   4 +-\n gcc/testsuite/rust/compile/issue-4226.rs      |   4 +-\n 8 files changed, 725 insertions(+), 526 deletions(-)\n create mode 100644 gcc/rust/checks/errors/rust-builtin-attribute-checker.cc\n create mode 100644 gcc/rust/checks/errors/rust-builtin-attribute-checker.h","diff":"diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in\nindex 4c394d39c0b..fc11abf1f1c 100644\n--- a/gcc/rust/Make-lang.in\n+++ b/gcc/rust/Make-lang.in\n@@ -126,6 +126,7 @@ GRS_OBJS = \\\n     rust/rust-hir.o \\\n     rust/rust-hir-map.o \\\n     rust/rust-attributes.o \\\n+    rust/rust-builtin-attribute-checker.o \\\n     rust/rust-keyword-values.o \\\n     rust/rust-abi.o \\\n     rust/rust-token-converter.o \\\ndiff --git a/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc b/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc\nnew file mode 100644\nindex 00000000000..8cb251f7686\n--- /dev/null\n+++ b/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc\n@@ -0,0 +1,602 @@\n+// Copyright (C) 2026 Free Software Foundation, Inc.\n+\n+// This file is part of GCC.\n+\n+// GCC is free software; you can redistribute it and/or modify it under\n+// the terms of the GNU General Public License as published by the Free\n+// Software Foundation; either version 3, or (at your option) any later\n+// version.\n+\n+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n+// WARRANTY; without even the implied warranty of MERCHANTABILITY or\n+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n+// for more details.\n+\n+// You should have received a copy of the GNU General Public License\n+// along with GCC; see the file COPYING3.  If not see\n+// <http://www.gnu.org/licenses/>.\n+\n+#include \"rust-builtin-attribute-checker.h\"\n+#include \"optional.h\"\n+#include \"rust-attributes.h\"\n+#include \"rust-attribute-values.h\"\n+#include \"rust-session-manager.h\"\n+\n+namespace Rust {\n+namespace Analysis {\n+\n+using Attrs = Values::Attributes;\n+\n+void\n+check_inner_attribute (const AST::Attribute &attribute)\n+{\n+  auto result_opt = lookup_builtin (attribute);\n+  if (!result_opt.has_value ())\n+    return;\n+  auto result = result_opt.value ();\n+\n+  if (Attributes::valid_outer_attribute (result.name))\n+    rust_error_at (attribute.get_locus (),\n+\t\t   \"attribute cannot be used at crate level\");\n+}\n+\n+/**\n+ * Check that the string given to #[doc(alias = ...)] or #[doc(alias(...))] is\n+ * valid.\n+ *\n+ * This means no whitespace characters other than spaces and no quoting\n+ * characters.\n+ */\n+static void\n+check_doc_alias (const std::string &alias_input, const location_t locus)\n+{\n+  // FIXME: The locus here is for the whole attribute. Can we get the locus\n+  // of the alias input instead?\n+  for (auto c : alias_input)\n+    if ((ISSPACE (c) && c != ' ') || c == '\\'' || c == '\\\"')\n+      {\n+\tauto to_print = std::string (1, c);\n+\tswitch (c)\n+\t  {\n+\t  case '\\n':\n+\t    to_print = \"\\\\n\";\n+\t    break;\n+\t  case '\\t':\n+\t    to_print = \"\\\\t\";\n+\t    break;\n+\t  default:\n+\t    break;\n+\t  }\n+\trust_error_at (locus,\n+\t\t       \"invalid character used in %<#[doc(alias)]%> input: %qs\",\n+\t\t       to_print.c_str ());\n+      }\n+\n+  if (alias_input.empty ())\n+    return;\n+\n+  if (alias_input.front () == ' ' || alias_input.back () == ' ')\n+    rust_error_at (locus,\n+\t\t   \"%<#[doc(alias)]%> input cannot start or end with a space\");\n+}\n+\n+static void\n+check_doc_attribute (const AST::Attribute &attribute)\n+{\n+  if (!attribute.has_attr_input ())\n+    {\n+      rust_error_at (\n+\tattribute.get_locus (),\n+\t\"valid forms for the attribute are \"\n+\t\"%<#[doc(hidden|inline|...)]%> and %<#[doc = \\\" string \\\"]%>\");\n+      return;\n+    }\n+\n+  switch (attribute.get_attr_input ().get_attr_input_type ())\n+    {\n+    case AST::AttrInput::LITERAL:\n+    case AST::AttrInput::META_ITEM:\n+    case AST::AttrInput::EXPR:\n+      break;\n+      // FIXME: Handle them as well\n+\n+    case AST::AttrInput::TOKEN_TREE:\n+      {\n+\t// FIXME: This doesn't check for #[doc(alias(...))]\n+\tconst auto &option = static_cast<const AST::DelimTokenTree &> (\n+\t  attribute.get_attr_input ());\n+\tauto *meta_item = option.parse_to_meta_item ();\n+\n+\tfor (auto &item : meta_item->get_items ())\n+\t  {\n+\t    if (item->is_key_value_pair ())\n+\t      {\n+\t\tauto name_value\n+\t\t  = static_cast<AST::MetaNameValueStr *> (item.get ())\n+\t\t      ->get_name_value_pair ();\n+\n+\t\t// FIXME: Check for other stuff than #[doc(alias = ...)]\n+\t\tif (name_value.first.as_string () == \"alias\")\n+\t\t  check_doc_alias (name_value.second, attribute.get_locus ());\n+\t      }\n+\t  }\n+\tbreak;\n+      }\n+    }\n+}\n+\n+static void\n+check_deprecated_attribute (const AST::Attribute &attribute)\n+{\n+  if (!attribute.has_attr_input ())\n+    return;\n+\n+  const auto &input = attribute.get_attr_input ();\n+\n+  if (input.get_attr_input_type () != AST::AttrInput::META_ITEM)\n+    return;\n+\n+  auto &meta = static_cast<const AST::AttrInputMetaItemContainer &> (input);\n+\n+  for (auto &current : meta.get_items ())\n+    {\n+      switch (current->get_kind ())\n+\t{\n+\tcase AST::MetaItemInner::Kind::MetaItem:\n+\t  {\n+\t    auto *meta_item = static_cast<AST::MetaItem *> (current.get ());\n+\n+\t    switch (meta_item->get_item_kind ())\n+\t      {\n+\t      case AST::MetaItem::ItemKind::NameValueStr:\n+\t\t{\n+\t\t  auto *nv = static_cast<AST::MetaNameValueStr *> (meta_item);\n+\n+\t\t  const std::string key = nv->get_name ().as_string ();\n+\n+\t\t  if (key != \"since\" && key != \"note\")\n+\t\t    {\n+\t\t      rust_error_at (nv->get_locus (), \"unknown meta item %qs\",\n+\t\t\t\t     key.c_str ());\n+\t\t      rust_inform (nv->get_locus (),\n+\t\t\t\t   \"expected one of %<since%>, %<note%>\");\n+\t\t    }\n+\t\t}\n+\t\tbreak;\n+\n+\t      case AST::MetaItem::ItemKind::Path:\n+\t\t{\n+\t\t  // #[deprecated(a,a)]\n+\t\t  auto *p = static_cast<AST::MetaItemPath *> (meta_item);\n+\n+\t\t  std::string ident = p->get_path ().as_string ();\n+\n+\t\t  rust_error_at (p->get_locus (), \"unknown meta item %qs\",\n+\t\t\t\t ident.c_str ());\n+\t\t  rust_inform (p->get_locus (),\n+\t\t\t       \"expected one of %<since%>, %<note%>\");\n+\t\t}\n+\t\tbreak;\n+\n+\t      case AST::MetaItem::ItemKind::Word:\n+\t\t{\n+\t\t  // #[deprecated(\"a\")]\n+\t\t  auto *w = static_cast<AST::MetaWord *> (meta_item);\n+\n+\t\t  rust_error_at (\n+\t\t    w->get_locus (),\n+\t\t    \"item in %<deprecated%> must be a key/value pair\");\n+\t\t}\n+\t\tbreak;\n+\n+\t      case AST::MetaItem::ItemKind::PathExpr:\n+\t\t{\n+\t\t  // #[deprecated(since=a)]\n+\t\t  auto *px = static_cast<AST::MetaItemPathExpr *> (meta_item);\n+\n+\t\t  rust_error_at (\n+\t\t    px->get_locus (),\n+\t\t    \"expected unsuffixed literal or identifier, found %qs\",\n+\t\t    px->get_expr ().as_string ().c_str ());\n+\t\t}\n+\t\tbreak;\n+\n+\t      case AST::MetaItem::ItemKind::Seq:\n+\t      case AST::MetaItem::ItemKind::ListPaths:\n+\t      case AST::MetaItem::ItemKind::ListNameValueStr:\n+\t      default:\n+\t\tgcc_unreachable ();\n+\t\tbreak;\n+\t      }\n+\t  }\n+\t  break;\n+\n+\tcase AST::MetaItemInner::Kind::LitExpr:\n+\tdefault:\n+\t  gcc_unreachable ();\n+\t  break;\n+\t}\n+    }\n+}\n+\n+static void\n+check_link_section_attribute (const AST::Attribute &attribute)\n+{\n+  if (!attribute.has_attr_input ())\n+    {\n+      rust_error_at (attribute.get_locus (),\n+\t\t     \"malformed %<link_section%> attribute input\");\n+      rust_inform (attribute.get_locus (),\n+\t\t   \"must be of the form: %<#[link_section = \\\"name\\\"]%>\");\n+    }\n+}\n+\n+static void\n+check_export_name_attribute (const AST::Attribute &attribute)\n+{\n+  if (!attribute.has_attr_input ())\n+    {\n+      rust_error_at (attribute.get_locus (),\n+\t\t     \"malformed %<export_name%> attribute input\");\n+      rust_inform (attribute.get_locus (),\n+\t\t   \"must be of the form: %<#[export_name = \\\"name\\\"]%>\");\n+      return;\n+    }\n+\n+  auto &attr_input = attribute.get_attr_input ();\n+  if (attr_input.get_attr_input_type ()\n+      == AST::AttrInput::AttrInputType::LITERAL)\n+    {\n+      auto &literal_expr\n+\t= static_cast<AST::AttrInputLiteral &> (attr_input).get_literal ();\n+      auto lit_type = literal_expr.get_lit_type ();\n+      switch (lit_type)\n+\t{\n+\tcase AST::Literal::LitType::STRING:\n+\tcase AST::Literal::LitType::RAW_STRING:\n+\tcase AST::Literal::LitType::BYTE_STRING:\n+\t  return;\n+\tdefault:\n+\t  break;\n+\t}\n+    }\n+\n+  rust_error_at (attribute.get_locus (), \"attribute must be a string literal\");\n+}\n+\n+static void\n+check_no_mangle_function (const AST::Attribute &attribute,\n+\t\t\t  const AST::Function &fun)\n+{\n+  if (attribute.has_attr_input ())\n+    {\n+      rust_error_at (attribute.get_locus (), ErrorCode::E0754,\n+\t\t     \"malformed %<no_mangle%> attribute input\");\n+      rust_inform (attribute.get_locus (),\n+\t\t   \"must be of the form: %<#[no_mangle]%>\");\n+    }\n+  if (!is_ascii_only (fun.get_function_name ().as_string ()))\n+    rust_error_at (fun.get_function_name ().get_locus (),\n+\t\t   \"the %<#[no_mangle]%> attribute requires ASCII identifier\");\n+}\n+\n+static void\n+check_lint_attribute (const AST::Attribute &attribute, const char *name)\n+{\n+  if (!attribute.has_attr_input ())\n+    {\n+      rust_error_at (attribute.get_locus (), \"malformed %qs attribute input\",\n+\t\t     name);\n+      rust_inform (attribute.get_locus (),\n+\t\t   \"must be of the form: %<#[%s(lint1, lint2, ...)]%>\", name);\n+    }\n+}\n+\n+/**\n+ * Emit an error when an attribute is attached\n+ * to an incompatable item type. e.g.:\n+ *\n+ * #[cold]\n+ * struct A(u8, u8);\n+ *\n+ * Note that \"#[derive]\" is handled\n+ * explicitly in rust-derive.cc\n+ */\n+static void\n+check_valid_attribute_for_item (const AST::Attribute &attr,\n+\t\t\t\tconst AST::Item &item)\n+{\n+  if (item.get_item_kind () != AST::Item::Kind::Function\n+      && (attr.get_path () == Values::Attributes::TARGET_FEATURE\n+\t  || attr.get_path () == Values::Attributes::COLD\n+\t  || attr.get_path () == Values::Attributes::INLINE))\n+    {\n+      rust_error_at (attr.get_locus (),\n+\t\t     \"the %<#[%s]%> attribute may only be applied to functions\",\n+\t\t     attr.get_path ().as_string ().c_str ());\n+    }\n+  else if (attr.get_path () == Values::Attributes::REPR\n+\t   && item.get_item_kind () != AST::Item::Kind::Enum\n+\t   && item.get_item_kind () != AST::Item::Kind::Union\n+\t   && item.get_item_kind () != AST::Item::Kind::Struct)\n+    {\n+      rust_error_at (attr.get_locus (),\n+\t\t     \"the %<#[%s]%> attribute may only be applied \"\n+\t\t     \"to structs, enums and unions\",\n+\t\t     attr.get_path ().as_string ().c_str ());\n+    }\n+}\n+\n+BuiltinAttributeChecker::BuiltinAttributeChecker () {}\n+\n+void\n+BuiltinAttributeChecker::go (AST::Crate &crate)\n+{\n+  visit (crate);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::Crate &crate)\n+{\n+  for (auto &attr : crate.get_inner_attrs ())\n+    {\n+      check_inner_attribute (attr);\n+    }\n+\n+  AST::DefaultASTVisitor::visit (crate);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::Attribute &attribute)\n+{\n+  if (auto builtin = lookup_builtin (attribute))\n+    {\n+      auto result = builtin.value ();\n+      // TODO: Add checks here for each builtin attribute\n+      // TODO: Have an enum of builtins as well, switching on strings is\n+      // annoying and costly\n+      if (result.name == Attrs::DOC)\n+\tcheck_doc_attribute (attribute);\n+      else if (result.name == Attrs::DEPRECATED)\n+\tcheck_deprecated_attribute (attribute);\n+    }\n+\n+  AST::DefaultASTVisitor::visit (attribute);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::Module &module)\n+{\n+  for (auto &attr : module.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, module);\n+\n+  AST::DefaultASTVisitor::visit (module);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::ExternCrate &crate)\n+{\n+  for (auto &attr : crate.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, crate);\n+\n+  AST::DefaultASTVisitor::visit (crate);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::UseDeclaration &declaration)\n+{\n+  for (auto &attr : declaration.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, declaration);\n+\n+  AST::DefaultASTVisitor::visit (declaration);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::Function &function)\n+{\n+  auto check_crate_type = [] (const char *name, AST::Attribute &attribute) {\n+    if (!Session::get_instance ().options.is_proc_macro ())\n+      rust_error_at (attribute.get_locus (),\n+\t\t     \"the %<#[%s]%> attribute is only usable with crates of \"\n+\t\t     \"the %<proc-macro%> crate type\",\n+\t\t     name);\n+  };\n+\n+  BuiltinAttrDefinition result;\n+  for (auto &attribute : function.get_outer_attrs ())\n+    {\n+      check_valid_attribute_for_item (attribute, function);\n+\n+      auto result = lookup_builtin (attribute);\n+      if (!result)\n+\treturn;\n+\n+      auto name = result->name.c_str ();\n+\n+      if (result->name == Attrs::PROC_MACRO_DERIVE)\n+\t{\n+\t  if (!attribute.has_attr_input ())\n+\t    {\n+\t      rust_error_at (attribute.get_locus (),\n+\t\t\t     \"malformed %qs attribute input\", name);\n+\t      rust_inform (\n+\t\tattribute.get_locus (),\n+\t\t\"must be of the form: %<#[proc_macro_derive(TraitName, \"\n+\t\t\"/*opt*/ attributes(name1, name2, ...))]%>\");\n+\t    }\n+\t  check_crate_type (name, attribute);\n+\t}\n+      else if (result->name == Attrs::PROC_MACRO\n+\t       || result->name == Attrs::PROC_MACRO_ATTRIBUTE)\n+\t{\n+\t  check_crate_type (name, attribute);\n+\t}\n+      else if (result->name == Attrs::TARGET_FEATURE)\n+\t{\n+\t  if (!attribute.has_attr_input ())\n+\t    {\n+\t      rust_error_at (attribute.get_locus (),\n+\t\t\t     \"malformed %<target_feature%> attribute input\");\n+\t      rust_inform (attribute.get_locus (),\n+\t\t\t   \"must be of the form: %<#[target_feature(enable = \"\n+\t\t\t   \"\\\"name\\\")]%>\");\n+\t    }\n+\t  else if (!function.get_qualifiers ().is_unsafe ())\n+\t    {\n+\t      rust_error_at (\n+\t\tattribute.get_locus (),\n+\t\t\"the %<#[target_feature]%> attribute can only be applied \"\n+\t\t\"to %<unsafe%> functions\");\n+\t    }\n+\t}\n+      else if (result->name == Attrs::NO_MANGLE)\n+\t{\n+\t  if (attribute.has_attr_input ())\n+\t    {\n+\t      rust_error_at (attribute.get_locus (),\n+\t\t\t     \"malformed %<no_mangle%> attribute input\");\n+\t      rust_inform (attribute.get_locus (),\n+\t\t\t   \"must be of the form: %<#[no_mangle]%>\");\n+\t    }\n+\t  else\n+\t    check_no_mangle_function (attribute, function);\n+\t}\n+      else if (result->name == Attrs::EXPORT_NAME)\n+\t{\n+\t  check_export_name_attribute (attribute);\n+\t}\n+      else if (result->name == Attrs::ALLOW || result->name == \"deny\"\n+\t       || result->name == \"warn\" || result->name == \"forbid\")\n+\t{\n+\t  check_lint_attribute (attribute, name);\n+\t}\n+      else if (result->name == Attrs::LINK_NAME)\n+\t{\n+\t  if (!attribute.has_attr_input ())\n+\t    {\n+\t      rust_error_at (attribute.get_locus (),\n+\t\t\t     \"malformed %<link_name%> attribute input\");\n+\t      rust_inform (attribute.get_locus (),\n+\t\t\t   \"must be of the form: %<#[link_name = \\\"name\\\"]%>\");\n+\t    }\n+\t}\n+      else if (result->name == Attrs::LINK_SECTION)\n+\t{\n+\t  check_link_section_attribute (attribute);\n+\t}\n+    }\n+\n+  AST::DefaultASTVisitor::visit (function);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::TypeAlias &alias)\n+{\n+  for (auto &attr : alias.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, alias);\n+\n+  AST::DefaultASTVisitor::visit (alias);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::StructStruct &struct_item)\n+{\n+  for (auto &attr : struct_item.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, struct_item);\n+\n+  AST::DefaultASTVisitor::visit (struct_item);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::TupleStruct &tuple_struct)\n+{\n+  for (auto &attr : tuple_struct.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, tuple_struct);\n+\n+  AST::DefaultASTVisitor::visit (tuple_struct);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::Enum &enumeration)\n+{\n+  for (auto &attr : enumeration.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, enumeration);\n+\n+  AST::DefaultASTVisitor::visit (enumeration);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::Union &u)\n+{\n+  for (auto &attr : u.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, u);\n+\n+  AST::DefaultASTVisitor::visit (u);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::ConstantItem &item)\n+{\n+  for (auto &attr : item.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, item);\n+\n+  AST::DefaultASTVisitor::visit (item);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::StaticItem &item)\n+{\n+  for (auto &attr : item.get_outer_attrs ())\n+    {\n+      check_valid_attribute_for_item (attr, item);\n+\n+      if (auto result = lookup_builtin (attr))\n+\t{\n+\t  if (result->name == Attrs::LINK_SECTION)\n+\t    check_link_section_attribute (attr);\n+\t  else if (result->name == Attrs::EXPORT_NAME)\n+\t    check_export_name_attribute (attr);\n+\t}\n+    }\n+\n+  AST::DefaultASTVisitor::visit (item);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::Trait &trait)\n+{\n+  for (auto &attr : trait.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, trait);\n+\n+  AST::DefaultASTVisitor::visit (trait);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::InherentImpl &impl)\n+{\n+  for (auto &attr : impl.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, impl);\n+\n+  AST::DefaultASTVisitor::visit (impl);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::TraitImpl &impl)\n+{\n+  for (auto &attr : impl.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, impl);\n+\n+  AST::DefaultASTVisitor::visit (impl);\n+}\n+\n+void\n+BuiltinAttributeChecker::visit (AST::ExternBlock &block)\n+{\n+  for (auto &attr : block.get_outer_attrs ())\n+    check_valid_attribute_for_item (attr, block);\n+\n+  AST::DefaultASTVisitor::visit (block);\n+}\n+\n+} // namespace Analysis\n+} // namespace Rust\ndiff --git a/gcc/rust/checks/errors/rust-builtin-attribute-checker.h b/gcc/rust/checks/errors/rust-builtin-attribute-checker.h\nnew file mode 100644\nindex 00000000000..6717de8abf6\n--- /dev/null\n+++ b/gcc/rust/checks/errors/rust-builtin-attribute-checker.h\n@@ -0,0 +1,58 @@\n+// Copyright (C) 2026 Free Software Foundation, Inc.\n+\n+// This file is part of GCC.\n+\n+// GCC is free software; you can redistribute it and/or modify it under\n+// the terms of the GNU General Public License as published by the Free\n+// Software Foundation; either version 3, or (at your option) any later\n+// version.\n+\n+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n+// WARRANTY; without even the implied warranty of MERCHANTABILITY or\n+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n+// for more details.\n+\n+// You should have received a copy of the GNU General Public License\n+// along with GCC; see the file COPYING3.  If not see\n+// <http://www.gnu.org/licenses/>.\n+\n+#ifndef RUST_BUILTIN_ATTRIBUTE_CHECKER_H\n+#define RUST_BUILTIN_ATTRIBUTE_CHECKER_H\n+\n+#include \"rust-ast-visitor.h\"\n+\n+namespace Rust {\n+namespace Analysis {\n+\n+class BuiltinAttributeChecker : public AST::DefaultASTVisitor\n+{\n+  using AST::DefaultASTVisitor::visit;\n+\n+public:\n+  BuiltinAttributeChecker ();\n+  void go (AST::Crate &crate);\n+  void visit (AST::Crate &crate) override;\n+  void visit (AST::Attribute &attribute) override;\n+\n+  // Items\n+  void visit (AST::Module &module) override;\n+  void visit (AST::ExternCrate &crate) override;\n+  void visit (AST::UseDeclaration &use_decl) override;\n+  void visit (AST::Function &function) override;\n+  void visit (AST::TypeAlias &type_alias) override;\n+  void visit (AST::StructStruct &struct_item) override;\n+  void visit (AST::TupleStruct &tuple_struct) override;\n+  void visit (AST::Enum &enum_item) override;\n+  void visit (AST::Union &union_item) override;\n+  void visit (AST::ConstantItem &const_item) override;\n+  void visit (AST::StaticItem &static_item) override;\n+  void visit (AST::Trait &trait) override;\n+  void visit (AST::InherentImpl &impl) override;\n+  void visit (AST::TraitImpl &impl) override;\n+  void visit (AST::ExternBlock &block) override;\n+};\n+\n+} // namespace Analysis\n+} // namespace Rust\n+\n+#endif /* ! RUST_BUILTIN_ATTRIBUTE_CHECKER_H */\ndiff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc\nindex 8b2fa5a2f50..6fc4463a934 100644\n--- a/gcc/rust/hir/rust-ast-lower-base.cc\n+++ b/gcc/rust/hir/rust-ast-lower-base.cc\n@@ -832,13 +832,7 @@ void\n ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &,\n \t\t\t\t\t    const AST::Attribute &attr)\n {\n-  if (!attr.has_attr_input ())\n-    {\n-      rust_error_at (attr.get_locus (),\n-\t\t     \"attribute must be of the form %qs or %qs\",\n-\t\t     \"#[doc(hidden|inline|...)]\", \"#[doc = string]\");\n-      return;\n-    }\n+  rust_assert (attr.has_attr_input ());\n \n   auto simple_doc_comment = attr.get_attr_input ().get_attr_input_type ()\n \t\t\t    == AST::AttrInput::AttrInputType::LITERAL;\ndiff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc\nindex 12e076d1df0..364ca081142 100644\n--- a/gcc/rust/rust-session-manager.cc\n+++ b/gcc/rust/rust-session-manager.cc\n@@ -60,6 +60,7 @@\n #include \"rust-borrow-checker.h\"\n #include \"rust-ast-validation.h\"\n #include \"rust-tyty-variance-analysis.h\"\n+#include \"rust-builtin-attribute-checker.h\"\n \n #include \"input.h\"\n #include \"selftest.h\"\n@@ -735,6 +736,8 @@ Session::compile_crate (const char *filename)\n \n   expansion (parsed_crate, name_resolution_ctx);\n \n+  Analysis::BuiltinAttributeChecker ().go (parsed_crate);\n+\n   AST::CollectLangItems ().go (parsed_crate);\n \n   rust_debug (\"\\033[0;31mSUCCESSFULLY FINISHED EXPANSION \\033[0m\");\ndiff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc\nindex 3b6587ffdaa..f03fa90d9c0 100644\n--- a/gcc/rust/util/rust-attributes.cc\n+++ b/gcc/rust/util/rust-attributes.cc\n@@ -29,40 +29,6 @@\n namespace Rust {\n namespace Analysis {\n \n-bool\n-Attributes::is_known (const std::string &attribute_path)\n-{\n-  const auto &lookup\n-    = BuiltinAttributeMappings::get ()->lookup_builtin (attribute_path);\n-\n-  return !lookup.is_error ();\n-}\n-\n-tl::optional<std::string>\n-Attributes::extract_string_literal (const AST::Attribute &attr)\n-{\n-  if (!attr.has_attr_input ())\n-    return tl::nullopt;\n-\n-  auto &attr_input = attr.get_attr_input ();\n-\n-  if (attr_input.get_attr_input_type ()\n-      != AST::AttrInput::AttrInputType::LITERAL)\n-    return tl::nullopt;\n-\n-  auto &literal_expr\n-    = static_cast<AST::AttrInputLiteral &> (attr_input).get_literal ();\n-\n-  auto lit_type = literal_expr.get_lit_type ();\n-\n-  // TODO: bring escape sequence handling out of lexing?\n-  if (lit_type != AST::Literal::LitType::STRING\n-      && lit_type != AST::Literal::LitType::RAW_STRING)\n-    return tl::nullopt;\n-\n-  return literal_expr.as_string ();\n-}\n-\n using Attrs = Values::Attributes;\n \n // https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248\n@@ -151,6 +117,45 @@ static const std::set<std::string> __outer_attributes\n      Attrs::LINK_NAME,\n      Attrs::LINK_SECTION};\n \n+bool\n+Attributes::is_known (const std::string &attribute_path)\n+{\n+  const auto &lookup\n+    = BuiltinAttributeMappings::get ()->lookup_builtin (attribute_path);\n+\n+  return !lookup.is_error ();\n+}\n+\n+bool\n+Attributes::valid_outer_attribute (const std::string &attribute_path)\n+{\n+  return __outer_attributes.find (attribute_path) != __outer_attributes.cend ();\n+}\n+\n+tl::optional<std::string>\n+Attributes::extract_string_literal (const AST::Attribute &attr)\n+{\n+  if (!attr.has_attr_input ())\n+    return tl::nullopt;\n+\n+  auto &attr_input = attr.get_attr_input ();\n+\n+  if (attr_input.get_attr_input_type ()\n+      != AST::AttrInput::AttrInputType::LITERAL)\n+    return tl::nullopt;\n+\n+  auto &literal_expr\n+    = static_cast<AST::AttrInputLiteral &> (attr_input).get_literal ();\n+\n+  auto lit_type = literal_expr.get_lit_type ();\n+\n+  // TODO: bring escape sequence handling out of lexing?\n+  if (lit_type != AST::Literal::LitType::STRING\n+      && lit_type != AST::Literal::LitType::RAW_STRING)\n+    return tl::nullopt;\n+\n+  return literal_expr.as_string ();\n+}\n BuiltinAttributeMappings *\n BuiltinAttributeMappings::get ()\n {\n@@ -189,20 +194,8 @@ AttributeChecker::go (AST::Crate &crate)\n   visit (crate);\n }\n \n-void\n-AttributeChecker::visit (AST::Crate &crate)\n-{\n-  for (auto &attr : crate.get_inner_attrs ())\n-    {\n-      check_inner_attribute (attr);\n-    }\n-\n-  for (auto &item : crate.items)\n-    item->accept_vis (*this);\n-}\n-\n tl::optional<BuiltinAttrDefinition>\n-identify_builtin (const AST::Attribute &attribute)\n+lookup_builtin (const AST::Attribute &attribute)\n {\n   auto &segments = attribute.get_path ().get_segments ();\n \n@@ -216,224 +209,10 @@ identify_builtin (const AST::Attribute &attribute)\n     segments.at (0).get_segment_name ());\n }\n \n-/**\n- * Check that the string given to #[doc(alias = ...)] or #[doc(alias(...))] is\n- * valid.\n- *\n- * This means no whitespace characters other than spaces and no quoting\n- * characters.\n- */\n-static void\n-check_doc_alias (const std::string &alias_input, const location_t locus)\n-{\n-  // FIXME: The locus here is for the whole attribute. Can we get the locus\n-  // of the alias input instead?\n-  for (auto c : alias_input)\n-    if ((ISSPACE (c) && c != ' ') || c == '\\'' || c == '\\\"')\n-      {\n-\tauto to_print = std::string (1, c);\n-\tswitch (c)\n-\t  {\n-\t  case '\\n':\n-\t    to_print = \"\\\\n\";\n-\t    break;\n-\t  case '\\t':\n-\t    to_print = \"\\\\t\";\n-\t    break;\n-\t  default:\n-\t    break;\n-\t  }\n-\trust_error_at (locus,\n-\t\t       \"invalid character used in %<#[doc(alias)]%> input: %qs\",\n-\t\t       to_print.c_str ());\n-      }\n-\n-  if (alias_input.empty ())\n-    return;\n-\n-  if (alias_input.front () == ' ' || alias_input.back () == ' ')\n-    rust_error_at (locus,\n-\t\t   \"%<#[doc(alias)]%> input cannot start or end with a space\");\n-}\n-\n-static void\n-check_doc_attribute (const AST::Attribute &attribute)\n-{\n-  if (!attribute.has_attr_input ())\n-    {\n-      rust_error_at (\n-\tattribute.get_locus (),\n-\t\"valid forms for the attribute are \"\n-\t\"%<#[doc(hidden|inline|...)]%> and %<#[doc = \\\" string \\\"]%>\");\n-      return;\n-    }\n-\n-  switch (attribute.get_attr_input ().get_attr_input_type ())\n-    {\n-    case AST::AttrInput::LITERAL:\n-    case AST::AttrInput::META_ITEM:\n-    case AST::AttrInput::EXPR:\n-      break;\n-      // FIXME: Handle them as well\n-\n-    case AST::AttrInput::TOKEN_TREE:\n-      {\n-\t// FIXME: This doesn't check for #[doc(alias(...))]\n-\tconst auto &option = static_cast<const AST::DelimTokenTree &> (\n-\t  attribute.get_attr_input ());\n-\tauto *meta_item = option.parse_to_meta_item ();\n-\n-\tfor (auto &item : meta_item->get_items ())\n-\t  {\n-\t    if (item->is_key_value_pair ())\n-\t      {\n-\t\tauto name_value\n-\t\t  = static_cast<AST::MetaNameValueStr *> (item.get ())\n-\t\t      ->get_name_value_pair ();\n-\n-\t\t// FIXME: Check for other stuff than #[doc(alias = ...)]\n-\t\tif (name_value.first.as_string () == \"alias\")\n-\t\t  check_doc_alias (name_value.second, attribute.get_locus ());\n-\t      }\n-\t  }\n-\tbreak;\n-      }\n-    }\n-}\n-\n-static void\n-check_deprecated_attribute (const AST::Attribute &attribute)\n-{\n-  if (!attribute.has_attr_input ())\n-    return;\n-\n-  const auto &input = attribute.get_attr_input ();\n-\n-  if (input.get_attr_input_type () != AST::AttrInput::META_ITEM)\n-    return;\n-\n-  auto &meta = static_cast<const AST::AttrInputMetaItemContainer &> (input);\n-\n-  for (auto &current : meta.get_items ())\n-    {\n-      switch (current->get_kind ())\n-\t{\n-\tcase AST::MetaItemInner::Kind::MetaItem:\n-\t  {\n-\t    auto *meta_item = static_cast<AST::MetaItem *> (current.get ());\n-\n-\t    switch (meta_item->get_item_kind ())\n-\t      {\n-\t      case AST::MetaItem::ItemKind::NameValueStr:\n-\t\t{\n-\t\t  auto *nv = static_cast<AST::MetaNameValueStr *> (meta_item);\n-\n-\t\t  const std::string key = nv->get_name ().as_string ();\n-\n-\t\t  if (key != \"since\" && key != \"note\")\n-\t\t    {\n-\t\t      rust_error_at (nv->get_locus (), \"unknown meta item %qs\",\n-\t\t\t\t     key.c_str ());\n-\t\t      rust_inform (nv->get_locus (),\n-\t\t\t\t   \"expected one of %<since%>, %<note%>\");\n-\t\t    }\n-\t\t}\n-\t\tbreak;\n-\n-\t      case AST::MetaItem::ItemKind::Path:\n-\t\t{\n-\t\t  // #[deprecated(a,a)]\n-\t\t  auto *p = static_cast<AST::MetaItemPath *> (meta_item);\n-\n-\t\t  std::string ident = p->get_path ().as_string ();\n-\n-\t\t  rust_error_at (p->get_locus (), \"unknown meta item %qs\",\n-\t\t\t\t ident.c_str ());\n-\t\t  rust_inform (p->get_locus (),\n-\t\t\t       \"expected one of %<since%>, %<note%>\");\n-\t\t}\n-\t\tbreak;\n-\n-\t      case AST::MetaItem::ItemKind::Word:\n-\t\t{\n-\t\t  // #[deprecated(\"a\")]\n-\t\t  auto *w = static_cast<AST::MetaWord *> (meta_item);\n-\n-\t\t  rust_error_at (\n-\t\t    w->get_locus (),\n-\t\t    \"item in %<deprecated%> must be a key/value pair\");\n-\t\t}\n-\t\tbreak;\n-\n-\t      case AST::MetaItem::ItemKind::PathExpr:\n-\t\t{\n-\t\t  // #[deprecated(since=a)]\n-\t\t  auto *px = static_cast<AST::MetaItemPathExpr *> (meta_item);\n-\n-\t\t  rust_error_at (\n-\t\t    px->get_locus (),\n-\t\t    \"expected unsuffixed literal or identifier, found %qs\",\n-\t\t    px->get_expr ().as_string ().c_str ());\n-\t\t}\n-\t\tbreak;\n-\n-\t      case AST::MetaItem::ItemKind::Seq:\n-\t      case AST::MetaItem::ItemKind::ListPaths:\n-\t      case AST::MetaItem::ItemKind::ListNameValueStr:\n-\t      default:\n-\t\tgcc_unreachable ();\n-\t\tbreak;\n-\t      }\n-\t  }\n-\t  break;\n-\n-\tcase AST::MetaItemInner::Kind::LitExpr:\n-\tdefault:\n-\t  gcc_unreachable ();\n-\t  break;\n-\t}\n-    }\n-}\n-\n-/**\n- * Emit an error when an attribute is attached\n- * to an incompatable item type. e.g.:\n- *\n- * #[cold]\n- * struct A(u8, u8);\n- *\n- * Note that \"#[derive]\" is handled\n- * explicitly in rust-derive.cc\n- */\n-static void\n-check_valid_attribute_for_item (const AST::Attribute &attr,\n-\t\t\t\tconst AST::Item &item)\n-{\n-  if (item.get_item_kind () != AST::Item::Kind::Function\n-      && (attr.get_path () == Values::Attributes::TARGET_FEATURE\n-\t  || attr.get_path () == Values::Attributes::COLD\n-\t  || attr.get_path () == Values::Attributes::INLINE))\n-    {\n-      rust_error_at (attr.get_locus (),\n-\t\t     \"the %<#[%s]%> attribute may only be applied to functions\",\n-\t\t     attr.get_path ().as_string ().c_str ());\n-    }\n-  else if (attr.get_path () == Values::Attributes::REPR\n-\t   && item.get_item_kind () != AST::Item::Kind::Enum\n-\t   && item.get_item_kind () != AST::Item::Kind::Union\n-\t   && item.get_item_kind () != AST::Item::Kind::Struct)\n-    {\n-      rust_error_at (attr.get_locus (),\n-\t\t     \"the %<#[%s]%> attribute may only be applied \"\n-\t\t     \"to structs, enums and unions\",\n-\t\t     attr.get_path ().as_string ().c_str ());\n-    }\n-}\n-\n static bool\n is_proc_macro_type (const AST::Attribute &attribute)\n {\n-  auto result_opt = identify_builtin (attribute);\n+  auto result_opt = lookup_builtin (attribute);\n   if (!result_opt.has_value ())\n     return false;\n   auto result = result_opt.value ();\n@@ -469,77 +248,6 @@ check_proc_macro_non_root (const AST::Attribute &attr, location_t loc)\n     }\n }\n \n-void\n-AttributeChecker::check_inner_attribute (const AST::Attribute &attribute)\n-{\n-  auto result_opt = identify_builtin (attribute);\n-  if (!result_opt.has_value ())\n-    return;\n-  auto result = result_opt.value ();\n-\n-  if (__outer_attributes.find (result.name) != __outer_attributes.end ())\n-    rust_error_at (attribute.get_locus (),\n-\t\t   \"attribute cannot be used at crate level\");\n-}\n-\n-static void\n-check_link_section_attribute (const AST::Attribute &attribute)\n-{\n-  if (!attribute.has_attr_input ())\n-    {\n-      rust_error_at (attribute.get_locus (),\n-\t\t     \"malformed %<link_section%> attribute input\");\n-      rust_inform (attribute.get_locus (),\n-\t\t   \"must be of the form: %<#[link_section = \\\"name\\\"]%>\");\n-    }\n-}\n-\n-static void\n-check_export_name_attribute (const AST::Attribute &attribute)\n-{\n-  if (!attribute.has_attr_input ())\n-    {\n-      rust_error_at (attribute.get_locus (),\n-\t\t     \"malformed %<export_name%> attribute input\");\n-      rust_inform (attribute.get_locus (),\n-\t\t   \"must be of the form: %<#[export_name = \\\"name\\\"]%>\");\n-      return;\n-    }\n-\n-  if (attribute.has_attr_input ())\n-    {\n-      auto &attr_input = attribute.get_attr_input ();\n-      if (attr_input.get_attr_input_type ()\n-\t  == AST::AttrInput::AttrInputType::LITERAL)\n-\t{\n-\t  auto &literal_expr\n-\t    = static_cast<AST::AttrInputLiteral &> (attr_input).get_literal ();\n-\t  auto lit_type = literal_expr.get_lit_type ();\n-\t  switch (lit_type)\n-\t    {\n-\t    case AST::Literal::LitType::STRING:\n-\t    case AST::Literal::LitType::RAW_STRING:\n-\t    case AST::Literal::LitType::BYTE_STRING:\n-\t      return;\n-\t    default:\n-\t      break;\n-\t    }\n-\t}\n-    }\n-}\n-\n-static void\n-check_lint_attribute (const AST::Attribute &attribute, const char *name)\n-{\n-  if (!attribute.has_attr_input ())\n-    {\n-      rust_error_at (attribute.get_locus (), \"malformed %qs attribute input\",\n-\t\t     name);\n-      rust_inform (attribute.get_locus (),\n-\t\t   \"must be of the form: %<#[%s(lint1, lint2, ...)]%>\", name);\n-    }\n-}\n-\n void\n AttributeChecker::visit (AST::Attribute &attribute)\n {\n@@ -552,18 +260,6 @@ AttributeChecker::visit (AST::Attribute &attribute)\n \treturn; // Do not emit errors for attribute that'll get stripped.\n     }\n \n-  if (auto builtin = identify_builtin (attribute))\n-    {\n-      auto result = builtin.value ();\n-      // TODO: Add checks here for each builtin attribute\n-      // TODO: Have an enum of builtins as well, switching on strings is\n-      // annoying and costly\n-      if (result.name == Attrs::DOC)\n-\tcheck_doc_attribute (attribute);\n-      else if (result.name == Attrs::DEPRECATED)\n-\tcheck_deprecated_attribute (attribute);\n-    }\n-\n   AST::DefaultASTVisitor::visit (attribute);\n }\n \n@@ -870,10 +566,7 @@ void\n AttributeChecker::visit (AST::Module &module)\n {\n   for (auto &attr : module.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, module);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n \n   for (auto &item : module.get_items ())\n     for (auto &attr : item->get_outer_attrs ())\n@@ -886,10 +579,7 @@ void\n AttributeChecker::visit (AST::ExternCrate &crate)\n {\n   for (auto &attr : crate.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, crate);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n }\n \n void\n@@ -908,124 +598,12 @@ void\n AttributeChecker::visit (AST::UseDeclaration &declaration)\n {\n   for (auto &attr : declaration.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, declaration);\n-      check_proc_macro_non_function (attr);\n-    }\n-}\n-\n-static void\n-check_no_mangle_function (const AST::Attribute &attribute,\n-\t\t\t  const AST::Function &fun)\n-{\n-  if (attribute.has_attr_input ())\n-    {\n-      rust_error_at (attribute.get_locus (), ErrorCode::E0754,\n-\t\t     \"malformed %<no_mangle%> attribute input\");\n-      rust_inform (attribute.get_locus (),\n-\t\t   \"must be of the form: %<#[no_mangle]%>\");\n-    }\n-  if (!is_ascii_only (fun.get_function_name ().as_string ()))\n-    rust_error_at (fun.get_function_name ().get_locus (),\n-\t\t   \"the %<#[no_mangle]%> attribute requires ASCII identifier\");\n+    check_proc_macro_non_function (attr);\n }\n \n void\n AttributeChecker::visit (AST::Function &fun)\n {\n-  for (auto &attr : fun.get_outer_attrs ())\n-    check_valid_attribute_for_item (attr, fun);\n-\n-  auto check_crate_type = [] (const char *name, AST::Attribute &attribute) {\n-    if (!Session::get_instance ().options.is_proc_macro ())\n-      rust_error_at (attribute.get_locus (),\n-\t\t     \"the %<#[%s]%> attribute is only usable with crates of \"\n-\t\t     \"the %<proc-macro%> crate type\",\n-\t\t     name);\n-  };\n-\n-  BuiltinAttrDefinition result;\n-  for (auto &attribute : fun.get_outer_attrs ())\n-    {\n-      auto result = identify_builtin (attribute);\n-      if (!result)\n-\treturn;\n-\n-      auto name = result->name.c_str ();\n-\n-      if (result->name == Attrs::PROC_MACRO_DERIVE)\n-\t{\n-\t  if (!attribute.has_attr_input ())\n-\t    {\n-\t      rust_error_at (attribute.get_locus (),\n-\t\t\t     \"malformed %qs attribute input\", name);\n-\t      rust_inform (\n-\t\tattribute.get_locus (),\n-\t\t\"must be of the form: %<#[proc_macro_derive(TraitName, \"\n-\t\t\"/*opt*/ attributes(name1, name2, ...))]%>\");\n-\t    }\n-\t  check_crate_type (name, attribute);\n-\t}\n-      else if (result->name == Attrs::PROC_MACRO\n-\t       || result->name == Attrs::PROC_MACRO_ATTRIBUTE)\n-\t{\n-\t  check_crate_type (name, attribute);\n-\t}\n-      else if (result->name == Attrs::TARGET_FEATURE)\n-\t{\n-\t  if (!attribute.has_attr_input ())\n-\t    {\n-\t      rust_error_at (attribute.get_locus (),\n-\t\t\t     \"malformed %<target_feature%> attribute input\");\n-\t      rust_inform (attribute.get_locus (),\n-\t\t\t   \"must be of the form: %<#[target_feature(enable = \"\n-\t\t\t   \"\\\"name\\\")]%>\");\n-\t    }\n-\t  else if (!fun.get_qualifiers ().is_unsafe ())\n-\t    {\n-\t      rust_error_at (\n-\t\tattribute.get_locus (),\n-\t\t\"the %<#[target_feature]%> attribute can only be applied \"\n-\t\t\"to %<unsafe%> functions\");\n-\t    }\n-\t}\n-      else if (result->name == Attrs::NO_MANGLE)\n-\t{\n-\t  if (attribute.has_attr_input ())\n-\t    {\n-\t      rust_error_at (attribute.get_locus (),\n-\t\t\t     \"malformed %<no_mangle%> attribute input\");\n-\t      rust_inform (attribute.get_locus (),\n-\t\t\t   \"must be of the form: %<#[no_mangle]%>\");\n-\t    }\n-\t  else\n-\t    check_no_mangle_function (attribute, fun);\n-\t}\n-      else if (result->name == Attrs::EXPORT_NAME)\n-\t{\n-\t  check_export_name_attribute (attribute);\n-\t}\n-      else if (result->name == Attrs::ALLOW || result->name == \"deny\"\n-\t       || result->name == \"warn\" || result->name == \"forbid\")\n-\t{\n-\t  check_lint_attribute (attribute, name);\n-\t}\n-      else if (result->name == Attrs::LINK_NAME)\n-\t{\n-\t  if (!attribute.has_attr_input ())\n-\t    {\n-\t      rust_error_at (attribute.get_locus (),\n-\t\t\t     \"malformed %<link_name%> attribute input\");\n-\t      rust_inform (attribute.get_locus (),\n-\t\t\t   \"must be of the form: %<#[link_name = \\\"name\\\"]%>\");\n-\t    }\n-\t}\n-      else if (result->name == Attrs::LINK_SECTION)\n-\t{\n-\t  check_link_section_attribute (attribute);\n-\t}\n-    }\n-\n   if (fun.has_body ())\n     fun.get_definition ().value ()->accept_vis (*this);\n }\n@@ -1034,10 +612,7 @@ void\n AttributeChecker::visit (AST::TypeAlias &alias)\n {\n   for (auto &attr : alias.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, alias);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n }\n \n void\n@@ -1045,7 +620,6 @@ AttributeChecker::visit (AST::StructStruct &struct_item)\n {\n   for (auto &attr : struct_item.get_outer_attrs ())\n     {\n-      check_valid_attribute_for_item (attr, struct_item);\n       check_proc_macro_non_function (attr);\n     }\n \n@@ -1056,10 +630,7 @@ void\n AttributeChecker::visit (AST::TupleStruct &tuplestruct)\n {\n   for (auto &attr : tuplestruct.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, tuplestruct);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n }\n \n void\n@@ -1082,48 +653,28 @@ void\n AttributeChecker::visit (AST::Enum &enumeration)\n {\n   for (auto &attr : enumeration.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, enumeration);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n }\n \n void\n AttributeChecker::visit (AST::Union &u)\n {\n   for (auto &attr : u.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, u);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n }\n \n void\n AttributeChecker::visit (AST::ConstantItem &item)\n {\n   for (auto &attr : item.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, item);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n }\n \n void\n AttributeChecker::visit (AST::StaticItem &item)\n {\n   for (auto &attr : item.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, item);\n-      check_proc_macro_non_function (attr);\n-\n-      if (auto result = identify_builtin (attr))\n-\t{\n-\t  if (result->name == Attrs::LINK_SECTION)\n-\t    check_link_section_attribute (attr);\n-\t  else if (result->name == Attrs::EXPORT_NAME)\n-\t    check_export_name_attribute (attr);\n-\t}\n-    }\n+    check_proc_macro_non_function (attr);\n }\n \n void\n@@ -1134,10 +685,8 @@ void\n AttributeChecker::visit (AST::Trait &trait)\n {\n   for (auto &attr : trait.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, trait);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n+\n   AST::DefaultASTVisitor::visit (trait);\n }\n \n@@ -1145,10 +694,7 @@ void\n AttributeChecker::visit (AST::InherentImpl &impl)\n {\n   for (auto &attr : impl.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, impl);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n \n   AST::DefaultASTVisitor::visit (impl);\n }\n@@ -1157,10 +703,8 @@ void\n AttributeChecker::visit (AST::TraitImpl &impl)\n {\n   for (auto &attr : impl.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, impl);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n+\n   AST::DefaultASTVisitor::visit (impl);\n }\n \n@@ -1176,10 +720,7 @@ void\n AttributeChecker::visit (AST::ExternBlock &block)\n {\n   for (auto &attr : block.get_outer_attrs ())\n-    {\n-      check_valid_attribute_for_item (attr, block);\n-      check_proc_macro_non_function (attr);\n-    }\n+    check_proc_macro_non_function (attr);\n }\n \n // rust-macro.h\ndiff --git a/gcc/rust/util/rust-attributes.h b/gcc/rust/util/rust-attributes.h\nindex 4bac820120c..3e5aea815e8 100644\n--- a/gcc/rust/util/rust-attributes.h\n+++ b/gcc/rust/util/rust-attributes.h\n@@ -29,6 +29,7 @@ class Attributes\n {\n public:\n   static bool is_known (const std::string &attribute_path);\n+  static bool valid_outer_attribute (const std::string &attribute_path);\n   static tl::optional<std::string>\n   extract_string_literal (const AST::Attribute &attr);\n };\n@@ -109,7 +110,6 @@ private:\n \n   // rust-ast.h\n   void visit (AST::Attribute &attribute) override;\n-  void visit (AST::Crate &crate) override;\n   void visit (AST::Token &tok) override;\n   void visit (AST::DelimTokenTree &delim_tok_tree) override;\n   void visit (AST::IdentifierExpr &ident_expr) override;\n@@ -277,7 +277,7 @@ private:\n };\n \n tl::optional<BuiltinAttrDefinition>\n-identify_builtin (const AST::Attribute &attribute);\n+lookup_builtin (const AST::Attribute &attribute);\n \n } // namespace Analysis\n } // namespace Rust\ndiff --git a/gcc/testsuite/rust/compile/issue-4226.rs b/gcc/testsuite/rust/compile/issue-4226.rs\nindex ec49606cfac..e0763eca02d 100644\n--- a/gcc/testsuite/rust/compile/issue-4226.rs\n+++ b/gcc/testsuite/rust/compile/issue-4226.rs\n@@ -1,6 +1,6 @@\n #![feature(no_core)]\n #![no_core]\n \n+// { dg-error \"valid forms for the attribute are ...doc.hidden.inline....... and ...doc ... string ...\" \"\" { target *-*-* } .+1 }\n #[doc]\n-// { dg-error \"attribute must be of the form ...doc.hidden.inline....... or ...doc = string..\" \"\" { target *-*-* } .-1 }\n-pub fn a(){}\n+pub fn a() {}\n","prefixes":["COMMITTED","22/43"]}