Line data Source code
1 : // Copyright (C) 2020-2026 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-attributes.h"
23 : #include "rust-ast-visitor.h"
24 : #include "rust-feature.h"
25 : #include "rust-ast-full.h"
26 : #include "rust-feature-store.h"
27 :
28 : namespace Rust {
29 :
30 : void
31 5268 : FeatureGate::check (AST::Crate &crate)
32 : {
33 5268 : auto &store = Features::EarlyFeatureGateStore::get ();
34 10547 : while (store.has_error ())
35 : {
36 11 : auto pair = store.get_error ();
37 11 : gate (pair.first, pair.second.locus, pair.second.message);
38 11 : }
39 5268 : visit (crate);
40 5268 : }
41 :
42 : void
43 5268 : FeatureGate::visit (AST::Crate &crate)
44 : {
45 5268 : AST::DefaultASTVisitor::visit (crate);
46 :
47 5693 : for (auto &ent : features.valid_lib_features)
48 : {
49 425 : const std::string &feature = ent.first;
50 425 : location_t locus = ent.second;
51 :
52 : // rustc treats these as valid,
53 : // but apparently has special handling for them
54 425 : if (feature == "libc" || feature == "test")
55 0 : continue;
56 :
57 425 : if (defined_lib_features.find (feature) != defined_lib_features.end ())
58 : {
59 : // TODO: emit warning if stable
60 420 : continue;
61 : }
62 :
63 5 : rust_error_at (locus, ErrorCode::E0635, "unknown feature %qs",
64 : feature.c_str ());
65 : }
66 20420 : for (const auto &attribute : crate.inner_attrs)
67 : {
68 15152 : check_no_core_attribute (attribute);
69 :
70 30304 : if (attribute.get_path ().as_string ()
71 15152 : == Values::Attributes::COMPILER_BUILTINS)
72 1 : gate (Feature::Name::COMPILER_BUILTINS, attribute.get_locus (),
73 2 : "the #[compiler_builtins] attribute is used to identify the "
74 : "compiler_builtins crate which contains compiler-rt intrinsics "
75 : "and will never be stable");
76 : }
77 5268 : }
78 :
79 : FeatureGate::GateResult
80 48565 : FeatureGate::gate (Feature::Name name, location_t loc,
81 : const std::string &error_msg)
82 : {
83 48565 : if (!features.valid_lang_features.count (name))
84 : {
85 22 : auto &feature = Feature::lookup (name);
86 :
87 22 : rust_error_at (loc, ErrorCode::E0658, "%s", error_msg.c_str ());
88 :
89 : // Mention the issue, if it is defined
90 22 : if (auto issue = feature.issue ())
91 13 : rust_inform (loc, "see issue #%u <%s%u> for more information", *issue,
92 13 : "https://github.com/rust-lang/rust/issues/", *issue);
93 :
94 22 : rust_inform (loc,
95 : "add %<#![feature(%s)]%> to the crate attributes to enable",
96 22 : feature.as_string ().c_str ());
97 :
98 22 : return GateResult::Gated;
99 : }
100 :
101 : return GateResult::Allowed;
102 : }
103 :
104 : void
105 2392 : FeatureGate::visit (AST::ExternBlock &block)
106 : {
107 2392 : if (block.has_abi ())
108 : {
109 2391 : const auto abi = block.get_abi ();
110 :
111 2391 : if (get_abi_from_string (abi) == ABI::INTRINSIC)
112 772 : gate (Feature::Name::INTRINSICS, block.get_locus (),
113 1544 : "intrinsics are subject to change");
114 2391 : }
115 2392 : AST::DefaultASTVisitor::visit (block);
116 2392 : }
117 :
118 : void
119 46 : FeatureGate::check_used_attribute (const AST::Attribute &attribute)
120 : {
121 46 : if (attribute.get_path ().as_string () == Values::Attributes::USED)
122 11 : if (attribute.has_attr_input ())
123 7 : gate (Feature::Name::USED_WITH_ARG, attribute.get_locus (),
124 14 : "malformed #[used] attribute input");
125 46 : }
126 :
127 : void
128 15152 : FeatureGate::check_no_core_attribute (const AST::Attribute &attribute)
129 : {
130 15152 : if (attribute.get_path ().as_string () == Values::Attributes::NO_CORE)
131 5248 : gate (Feature::Name::NO_CORE, attribute.get_locus (),
132 10496 : "no_core is experimental");
133 15152 : }
134 :
135 : void
136 168353 : FeatureGate::check_rustc_attri (const std::vector<AST::Attribute> &attributes)
137 : {
138 799747 : for (const AST::Attribute &attr : attributes)
139 : {
140 631394 : auto name = attr.get_path ().as_string ();
141 631394 : if (name.rfind ("rustc_", 0) == 0)
142 : {
143 28306 : gate (Feature::Name::RUSTC_ATTRS, attr.get_locus (),
144 56612 : "internal implementation detail");
145 : }
146 631394 : }
147 168353 : }
148 :
149 : void
150 120498 : FeatureGate::check_may_dangle_attribute (
151 : const std::vector<AST::Attribute> &attributes)
152 : {
153 120517 : for (const AST::Attribute &attr : attributes)
154 : {
155 19 : if (attr.get_path ().as_string () == Values::Attributes::MAY_DANGLE)
156 15 : gate (Feature::Name::DROPCK_EYEPATCH, attr.get_locus (),
157 30 : "`may_dangle` has unstable semantics and may be removed in the "
158 : "future");
159 : }
160 120498 : }
161 :
162 : void
163 190625 : FeatureGate::check_lang_item_attribute (
164 : const std::vector<AST::Attribute> &attributes)
165 : {
166 927521 : for (const AST::Attribute &attr : attributes)
167 : {
168 736896 : const auto &str_path = attr.get_path ().as_string ();
169 736896 : bool is_lang_item = str_path == Values::Attributes::LANG
170 4983 : && attr.has_attr_input ()
171 741879 : && attr.get_attr_input ().get_attr_input_type ()
172 736896 : == AST::AttrInput::AttrInputType::LITERAL;
173 :
174 4983 : if (is_lang_item)
175 4983 : gate (Feature::Name::LANG_ITEMS, attr.get_locus (),
176 9966 : "lang items are subject to change");
177 736896 : }
178 190625 : }
179 :
180 : void
181 181472 : FeatureGate::note_stability_attribute (
182 : const std::vector<AST::Attribute> &attributes)
183 : {
184 836118 : for (const AST::Attribute &attr : attributes)
185 : {
186 654646 : std::string attr_name = attr.get_path ().as_string ();
187 :
188 654646 : Stability stability;
189 :
190 654646 : if (attr_name == Values::Attributes::STABLE)
191 32119 : stability = Stability::STABLE;
192 622527 : else if (attr_name == Values::Attributes::UNSTABLE)
193 5996 : stability = Stability::UNSTABLE;
194 616531 : else if (attr_name == Values::Attributes::RUSTC_CONST_STABLE)
195 8890 : stability = Stability::STABLE;
196 607641 : else if (attr_name == Values::Attributes::RUSTC_CONST_UNSTABLE)
197 4744 : stability = Stability::UNSTABLE;
198 : else
199 602897 : continue;
200 :
201 51749 : if (attr.empty_input ())
202 : // TODO: error?
203 0 : continue;
204 :
205 51749 : auto &attr_input = attr.get_attr_input ();
206 51749 : if (attr_input.get_attr_input_type ()
207 : != AST::AttrInput::AttrInputType::TOKEN_TREE)
208 : // TODO: error?
209 0 : continue;
210 :
211 51749 : std::unique_ptr<AST::AttrInputMetaItemContainer> meta_item (
212 : static_cast<const AST::DelimTokenTree &> (attr_input)
213 51749 : .parse_to_meta_item ());
214 :
215 156646 : for (auto &item : meta_item->get_items ())
216 : {
217 : // TODO: more thorough error checking?
218 : // ~only the standard libraries should ever exercise this
219 104897 : if (item->is_key_value_pair ())
220 : {
221 104885 : auto &pair = static_cast<const AST::MetaNameValueStr &> (*item);
222 104885 : if (pair.get_name ().as_string () == "feature")
223 51749 : defined_lib_features.emplace (pair.get_value (), stability);
224 : }
225 : }
226 654646 : }
227 181472 : }
228 :
229 : void
230 9825 : FeatureGate::visit (AST::MacroRulesDefinition &rules_def)
231 : {
232 9825 : check_rustc_attri (rules_def.get_outer_attrs ());
233 9825 : note_stability_attribute (rules_def.get_outer_attrs ());
234 9825 : }
235 :
236 : void
237 171647 : FeatureGate::visit (AST::Function &function)
238 : {
239 171647 : if (!function.is_external ())
240 158528 : check_rustc_attri (function.get_outer_attrs ());
241 :
242 811063 : for (const AST::Attribute &attr : function.get_outer_attrs ())
243 : {
244 639416 : if (attr.get_path ().as_string () == "rustc_const_stable")
245 : {
246 8890 : gate (Feature::Name::STAGED_API, attr.get_locus (),
247 17780 : "stability attributes may not be used outside of the standard "
248 : "library");
249 : }
250 : }
251 :
252 171647 : check_lang_item_attribute (function.get_outer_attrs ());
253 :
254 171647 : note_stability_attribute (function.get_outer_attrs ());
255 :
256 171647 : AST::DefaultASTVisitor::visit (function);
257 171647 : }
258 :
259 : void
260 16 : FeatureGate::visit (AST::ExternalTypeItem &item)
261 : {
262 : // TODO(mxlol233): The gating needs a complete visiting chain to activate
263 : // `AST::ExternalTypeItem`.
264 16 : gate (Feature::Name::EXTERN_TYPES, item.get_locus (),
265 16 : "extern types are experimental");
266 :
267 16 : AST::DefaultASTVisitor::visit (item);
268 16 : }
269 :
270 : void
271 88967 : FeatureGate::visit (AST::TraitImpl &impl)
272 : {
273 88967 : if (impl.is_exclam ())
274 181 : gate (Feature::Name::NEGATIVE_IMPLS, impl.get_locus (),
275 362 : "negative_impls are not yet implemented");
276 :
277 88967 : AST::DefaultASTVisitor::visit (impl);
278 88967 : }
279 :
280 : void
281 6274 : FeatureGate::visit (AST::Trait &trait)
282 : {
283 6274 : if (trait.is_auto ())
284 78 : gate (Feature::Name::OPTIN_BUILTIN_TRAITS, trait.get_locus (),
285 156 : "auto traits are experimental and possibly buggy");
286 6274 : check_lang_item_attribute (trait.get_outer_attrs ());
287 6274 : AST::DefaultASTVisitor::visit (trait);
288 6274 : }
289 :
290 : void
291 19 : FeatureGate::visit (AST::BoxExpr &expr)
292 : {
293 19 : gate (
294 : Feature::Name::BOX_SYNTAX, expr.get_locus (),
295 19 : "box expression syntax is experimental; you can call `Box::new` instead");
296 19 : AST::DefaultASTVisitor::visit (expr);
297 19 : }
298 :
299 : void
300 15016 : FeatureGate::visit (AST::LifetimeParam &lifetime_param)
301 : {
302 15016 : check_may_dangle_attribute (lifetime_param.get_outer_attrs ());
303 15016 : AST::DefaultASTVisitor::visit (lifetime_param);
304 15016 : }
305 :
306 : void
307 1037 : FeatureGate::visit (AST::ConstGenericParam &const_param)
308 : {
309 1037 : check_may_dangle_attribute (const_param.get_outer_attrs ());
310 1037 : AST::DefaultASTVisitor::visit (const_param);
311 1037 : }
312 :
313 : void
314 104445 : FeatureGate::visit (AST::TypeParam ¶m)
315 : {
316 104445 : check_may_dangle_attribute (param.get_outer_attrs ());
317 104445 : AST::DefaultASTVisitor::visit (param);
318 104445 : }
319 :
320 : void
321 37929 : FeatureGate::visit (AST::BorrowExpr &expr)
322 : {
323 37929 : if (expr.is_raw_borrow ())
324 11 : gate (Feature::Name::RAW_REF_OP, expr.get_locus (),
325 22 : "raw address of syntax is experimental");
326 37929 : }
327 :
328 : void
329 934 : FeatureGate::visit (AST::RangePattern &pattern)
330 : {
331 934 : if (pattern.get_range_kind () == AST::RangeKind::EXCLUDED)
332 23 : gate (Feature::Name::EXCLUSIVE_RANGE_PATTERN, pattern.get_locus (),
333 46 : "exclusive range pattern syntax is experimental");
334 934 : }
335 :
336 : void
337 845 : FeatureGate::visit (AST::UseTreeGlob &use)
338 : {
339 : // At the moment, UseTrees do not have outer attributes, but they should. we
340 : // need to eventually gate `#[prelude_import]` on use-trees based on the
341 : // #[feature(prelude_import)]
342 845 : }
343 :
344 : void
345 9091 : FeatureGate::visit (AST::StructStruct &struct_item)
346 : {
347 9091 : check_lang_item_attribute (struct_item.get_outer_attrs ());
348 9091 : AST::DefaultASTVisitor::visit (struct_item);
349 9091 : }
350 :
351 : void
352 1318 : FeatureGate::visit (AST::TraitItemType &trait_item_type)
353 : {
354 1318 : check_lang_item_attribute (trait_item_type.get_outer_attrs ());
355 1318 : AST::DefaultASTVisitor::visit (trait_item_type);
356 1318 : }
357 :
358 : void
359 1041 : FeatureGate::visit (AST::Enum &enum_item)
360 : {
361 1041 : check_lang_item_attribute (enum_item.get_outer_attrs ());
362 1041 : AST::DefaultASTVisitor::visit (enum_item);
363 1041 : }
364 :
365 : void
366 1254 : FeatureGate::visit (AST::EnumItem &enum_variant)
367 : {
368 1254 : check_lang_item_attribute (enum_variant.get_outer_attrs ());
369 1254 : AST::DefaultASTVisitor::visit (enum_variant);
370 1254 : }
371 : void
372 517 : FeatureGate::visit (AST::StaticItem &static_item)
373 : {
374 563 : for (const auto &attr : static_item.get_outer_attrs ())
375 46 : check_used_attribute (attr);
376 :
377 517 : AST::DefaultASTVisitor::visit (static_item);
378 517 : }
379 :
380 : void
381 2 : FeatureGate::visit (AST::TraitAlias &trait_alias)
382 : {
383 2 : gate (Feature::Name::TRAIT_ALIAS, trait_alias.get_locus (),
384 2 : "trait aliases are experimental");
385 :
386 2 : AST::DefaultASTVisitor::visit (trait_alias);
387 2 : }
388 :
389 : void
390 889781 : FeatureGate::visit (AST::Attribute &attr)
391 : {
392 889781 : if (attr.get_path ().as_string () == "cfi_encoding")
393 2 : if (gate (Feature::Name::CFI_ENCODING, attr.get_locus (),
394 4 : "#[cfi_encoding] is an experimental feature")
395 2 : == GateResult::Allowed)
396 1 : rust_warning_at (
397 : attr.get_locus (), 0,
398 : "the %<#[cfi_encoding]%> attribute is currently ignored and "
399 : "does nothing - we are waiting on a patchset to land "
400 : "into GCC as the KCFI functionality is not present yet");
401 :
402 889781 : AST::DefaultASTVisitor::visit (attr);
403 889781 : }
404 :
405 : } // namespace Rust
|