| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#include "rust-feature-gate.h" |
| 20 |
#include "rust-abi.h" |
| 21 |
#include "rust-attribute-values.h" |
| 22 |
#include "rust-ast-visitor.h" |
| 23 |
#include "rust-feature.h" |
| 24 |
#include "rust-ast-full.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
|
| 28 |
void |
| 29 |
FeatureGate::check (AST::Crate &crate) |
| 30 |
{ |
| 31 |
visit (crate); |
| 32 |
} |
| 33 |
|
| 34 |
void |
| 35 |
FeatureGate::visit (AST::Crate &crate) |
| 36 |
{ |
| 37 |
valid_features.clear (); |
| 38 |
|
| 39 |
for (const auto &attr : crate.inner_attrs) |
| 40 |
{ |
| 41 |
if (attr.get_path ().as_string () == "feature") |
| 42 |
{ |
| 43 |
// check for empty feature, such as `#![feature], this is an error |
| 44 |
if (attr.empty_input ()) |
| 45 |
{ |
| 46 |
rust_error_at (attr.get_locus (), ErrorCode::E0556, |
| 47 |
"malformed %<feature%> attribute input"); |
| 48 |
continue; |
| 49 |
} |
| 50 |
const auto &attr_input = attr.get_attr_input (); |
| 51 |
auto type = attr_input.get_attr_input_type (); |
| 52 |
if (type == AST::AttrInput::AttrInputType::TOKEN_TREE) |
| 53 |
{ |
| 54 |
const auto &option = static_cast<const AST::DelimTokenTree &> ( |
| 55 |
attr.get_attr_input ()); |
| 56 |
std::unique_ptr<AST::AttrInputMetaItemContainer> meta_item ( |
| 57 |
option.parse_to_meta_item ()); |
| 58 |
for (const auto &item : meta_item->get_items ()) |
| 59 |
{ |
| 60 |
const auto &name_str = item->as_string (); |
| 61 |
auto tname = Feature::as_name (name_str); |
| 62 |
if (tname.has_value ()) |
| 63 |
{ |
| 64 |
auto name = tname.value (); |
| 65 |
valid_features.insert (name); |
| 66 |
} |
| 67 |
|
| 68 |
else |
| 69 |
rust_error_at (item->get_locus (), ErrorCode::E0635, |
| 70 |
"unknown feature %qs", name_str.c_str ()); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
AST::DefaultASTVisitor::visit (crate); |
| 77 |
} |
| 78 |
|
| 79 |
void |
| 80 |
FeatureGate::gate (Feature::Name name, location_t loc, |
| 81 |
const std::string &error_msg) |
| 82 |
{ |
| 83 |
if (!valid_features.count (name)) |
| 84 |
{ |
| 85 |
auto feature = Feature::create (name); |
| 86 |
if (auto issue = feature.issue ()) |
| 87 |
{ |
| 88 |
auto issue_number = issue.value (); |
| 89 |
const char *fmt_str |
| 90 |
= "%s. see issue %u " |
| 91 |
"<https://github.com/rust-lang/rust/issues/%u> for more " |
| 92 |
"information. add `#![feature(%s)]` to the crate attributes to " |
| 93 |
"enable."; |
| 94 |
rust_error_at (loc, ErrorCode::E0658, fmt_str, error_msg.c_str (), |
| 95 |
issue_number, issue_number, |
| 96 |
feature.as_string ().c_str ()); |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
const char *fmt_str |
| 101 |
= "%s. add `#![feature(%s)]` to the crate attributes to enable."; |
| 102 |
rust_error_at (loc, ErrorCode::E0658, fmt_str, error_msg.c_str (), |
| 103 |
feature.as_string ().c_str ()); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
void |
| 109 |
FeatureGate::visit (AST::ExternBlock &block) |
| 110 |
{ |
| 111 |
if (block.has_abi ()) |
| 112 |
{ |
| 113 |
const auto abi = block.get_abi (); |
| 114 |
|
| 115 |
if (get_abi_from_string (abi) == ABI::INTRINSIC) |
| 116 |
gate (Feature::Name::INTRINSICS, block.get_locus (), |
| 117 |
"intrinsics are subject to change"); |
| 118 |
} |
| 119 |
AST::DefaultASTVisitor::visit (block); |
| 120 |
} |
| 121 |
|
| 122 |
void |
| 123 |
FeatureGate::check_rustc_attri (const std::vector<AST::Attribute> &attributes) |
| 124 |
{ |
| 125 |
for (const AST::Attribute &attr : attributes) |
| 126 |
{ |
| 127 |
auto name = attr.get_path ().as_string (); |
| 128 |
if (name.rfind ("rustc_", 0) == 0) |
| 129 |
{ |
| 130 |
gate (Feature::Name::RUSTC_ATTRS, attr.get_locus (), |
| 131 |
"internal implementation detail"); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void |
| 137 |
FeatureGate::check_may_dangle_attribute ( |
| 138 |
const std::vector<AST::Attribute> &attributes) |
| 139 |
{ |
| 140 |
for (const AST::Attribute &attr : attributes) |
| 141 |
{ |
| 142 |
if (attr.get_path ().as_string () == Values::Attributes::MAY_DANGLE) |
| 143 |
gate (Feature::Name::DROPCK_EYEPATCH, attr.get_locus (), |
| 144 |
"`may_dangle` has unstable semantics and may be removed in the " |
| 145 |
"future"); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void |
| 150 |
FeatureGate::visit (AST::MacroRulesDefinition &rules_def) |
| 151 |
{ |
| 152 |
check_rustc_attri (rules_def.get_outer_attrs ()); |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
FeatureGate::visit (AST::Function &function) |
| 157 |
{ |
| 158 |
if (!function.is_external ()) |
| 159 |
check_rustc_attri (function.get_outer_attrs ()); |
| 160 |
|
| 161 |
AST::DefaultASTVisitor::visit (function); |
| 162 |
} |
| 163 |
|
| 164 |
void |
| 165 |
FeatureGate::visit (AST::ExternalTypeItem &item) |
| 166 |
{ |
| 167 |
// TODO(mxlol233): The gating needs a complete visiting chain to activate |
| 168 |
// `AST::ExternalTypeItem`. |
| 169 |
gate (Feature::Name::EXTERN_TYPES, item.get_locus (), |
| 170 |
"extern types are experimental"); |
| 171 |
} |
| 172 |
|
| 173 |
void |
| 174 |
FeatureGate::visit (AST::TraitImpl &impl) |
| 175 |
{ |
| 176 |
if (impl.is_exclam ()) |
| 177 |
gate (Feature::Name::NEGATIVE_IMPLS, impl.get_locus (), |
| 178 |
"negative_impls are not yet implemented"); |
| 179 |
|
| 180 |
AST::DefaultASTVisitor::visit (impl); |
| 181 |
} |
| 182 |
|
| 183 |
void |
| 184 |
FeatureGate::visit (AST::Trait &trait) |
| 185 |
{ |
| 186 |
if (trait.is_auto ()) |
| 187 |
gate (Feature::Name::AUTO_TRAITS, trait.get_locus (), |
| 188 |
"auto traits are experimental and possibly buggy"); |
| 189 |
AST::DefaultASTVisitor::visit (trait); |
| 190 |
} |
| 191 |
|
| 192 |
void |
| 193 |
FeatureGate::visit (AST::BoxExpr &expr) |
| 194 |
{ |
| 195 |
gate ( |
| 196 |
Feature::Name::BOX_SYNTAX, expr.get_locus (), |
| 197 |
"box expression syntax is experimental; you can call `Box::new` instead"); |
| 198 |
AST::DefaultASTVisitor::visit (expr); |
| 199 |
} |
| 200 |
|
| 201 |
void |
| 202 |
FeatureGate::visit (AST::LifetimeParam &lifetime_param) |
| 203 |
{ |
| 204 |
check_may_dangle_attribute (lifetime_param.get_outer_attrs ()); |
| 205 |
AST::DefaultASTVisitor::visit (lifetime_param); |
| 206 |
} |
| 207 |
|
| 208 |
void |
| 209 |
FeatureGate::visit (AST::ConstGenericParam &const_param) |
| 210 |
{ |
| 211 |
check_may_dangle_attribute (const_param.get_outer_attrs ()); |
| 212 |
AST::DefaultASTVisitor::visit (const_param); |
| 213 |
} |
| 214 |
|
| 215 |
void |
| 216 |
FeatureGate::visit (AST::TypeParam ¶m) |
| 217 |
{ |
| 218 |
check_may_dangle_attribute (param.get_outer_attrs ()); |
| 219 |
AST::DefaultASTVisitor::visit (param); |
| 220 |
} |
| 221 |
|
| 222 |
void |
| 223 |
FeatureGate::visit (AST::BorrowExpr &expr) |
| 224 |
{ |
| 225 |
if (expr.is_raw_borrow ()) |
| 226 |
gate (Feature::Name::RAW_REF_OP, expr.get_locus (), |
| 227 |
"raw address of syntax is experimental"); |
| 228 |
} |
| 229 |
|
| 230 |
void |
| 231 |
FeatureGate::visit (AST::RangePattern &pattern) |
| 232 |
{ |
| 233 |
if (pattern.get_range_kind () == AST::RangeKind::EXCLUDED) |
| 234 |
gate (Feature::Name::EXCLUSIVE_RANGE_PATTERN, pattern.get_locus (), |
| 235 |
"exclusive range pattern syntax is experimental"); |
| 236 |
} |
| 237 |
|
| 238 |
void |
| 239 |
FeatureGate::visit (AST::UseTreeGlob &use) |
| 240 |
{ |
| 241 |
// At the moment, UseTrees do not have outer attributes, but they should. we |
| 242 |
// need to eventually gate `#[prelude_import]` on use-trees based on the |
| 243 |
// #[feature(prelude_import)] |
| 244 |
} |
| 245 |
|
| 246 |
} // namespace Rust |