Skip to content

Commit 084789e

Browse files
committed
feature-gate: Cleanup visitor and constructor
This commit turns a few of the fields into `tl::optional<T>` and cleanups the constructor with a couple default parameters. We can also reuse the variable that we are `switch`ing on, instead of repeating the enum variants each time. Most importantly, the `FeatureGate` visitor now uses the `DefaultAstVisitor` correctly, and will now visit nested items. This means that we have to fix a bunch of the testsuite as some feature attributes were missing. gcc/rust/ChangeLog: * checks/errors/rust-feature.cc (Feature::create): Reuse variable, remove now optional parameters from constructor. * checks/errors/rust-feature.h: Cleanup class definition. * checks/errors/rust-feature-gate.cc (FeatureGate::gate): Use optional. * checks/errors/rust-feature-gate.h: Cleanup visitor implementation. gcc/testsuite/ChangeLog: * rust/compile/assume.rs: Add missing feature attribute. * rust/compile/issue-1901.rs: Likewise. * rust/compile/issue-1981.rs: Likewise. * rust/compile/sizeof-stray-infer-var-bug.rs: Likewise. * rust/compile/torture/intrinsics-8.rs: Likewise. * rust/compile/torture/transmute-size-check-1.rs: Likewise. * rust/compile/torture/transmute1.rs: Likewise. * rust/compile/torture/uninit-intrinsic-1.rs: Likewise. * rust/execute/torture/issue-1436.rs: Likewise. * rust/execute/torture/issue-2583.rs: Likewise.
1 parent 06e8b82 commit 084789e

15 files changed

+90
-169
lines changed

gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.lock

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gcc/rust/checks/errors/rust-feature-gate.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "rust-attribute-values.h"
2222
#include "rust-ast-visitor.h"
2323
#include "rust-feature.h"
24+
#include "rust-ast-full.h"
2425

2526
namespace Rust {
2627

@@ -75,16 +76,17 @@ FeatureGate::gate (Feature::Name name, location_t loc,
7576
if (!valid_features.count (name))
7677
{
7778
auto feature = Feature::create (name);
78-
auto issue = feature.issue ();
79-
if (issue > 0)
79+
if (auto issue = feature.issue ())
8080
{
81+
auto issue_number = issue.value ();
8182
const char *fmt_str
8283
= "%s. see issue %u "
8384
"<https://github.com/rust-lang/rust/issues/%u> for more "
8485
"information. add `#![feature(%s)]` to the crate attributes to "
8586
"enable.";
8687
rust_error_at (loc, ErrorCode::E0658, fmt_str, error_msg.c_str (),
87-
issue, issue, feature.as_string ().c_str ());
88+
issue_number, issue_number,
89+
feature.as_string ().c_str ());
8890
}
8991
else
9092
{
@@ -169,7 +171,7 @@ FeatureGate::visit (AST::TraitImpl &impl)
169171
"negative_impls are not yet implemented");
170172

171173
AST::DefaultASTVisitor::visit (impl);
172-
};
174+
}
173175

174176
void
175177
FeatureGate::visit (AST::BoxExpr &expr)

gcc/rust/checks/errors/rust-feature-gate.h

-136
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#define RUST_FEATURE_GATE_H
2121

2222
#include "rust-ast-visitor.h"
23-
#include "rust-ast-full.h"
2423
#include "rust-feature.h"
2524

2625
namespace Rust {
@@ -35,153 +34,18 @@ class FeatureGate : public AST::DefaultASTVisitor
3534
void check (AST::Crate &crate);
3635
void visit (AST::Crate &crate) override;
3736

38-
void visit (AST::Token &tok) override {}
39-
void visit (AST::DelimTokenTree &delim_tok_tree) override {}
40-
void visit (AST::AttrInputMetaItemContainer &input) override {}
41-
void visit (AST::IdentifierExpr &ident_expr) override {}
42-
void visit (AST::Lifetime &lifetime) override {}
4337
void visit (AST::LifetimeParam &lifetime_param) override;
4438
void visit (AST::ConstGenericParam &const_param) override;
45-
void visit (AST::PathInExpression &path) override {}
46-
void visit (AST::TypePathSegment &segment) override {}
47-
void visit (AST::TypePathSegmentGeneric &segment) override {}
48-
void visit (AST::TypePathSegmentFunction &segment) override {}
49-
void visit (AST::TypePath &path) override {}
50-
void visit (AST::QualifiedPathInExpression &path) override {}
51-
void visit (AST::QualifiedPathInType &path) override {}
52-
void visit (AST::LiteralExpr &expr) override {}
53-
void visit (AST::AttrInputLiteral &attr_input) override {}
54-
void visit (AST::AttrInputMacro &attr_input) override {}
55-
void visit (AST::MetaItemLitExpr &meta_item) override {}
56-
void visit (AST::MetaItemPathLit &meta_item) override {}
5739
void visit (AST::BorrowExpr &expr) override;
58-
void visit (AST::DereferenceExpr &expr) override {}
59-
void visit (AST::ErrorPropagationExpr &expr) override {}
60-
void visit (AST::NegationExpr &expr) override {}
61-
void visit (AST::ArithmeticOrLogicalExpr &expr) override {}
62-
void visit (AST::ComparisonExpr &expr) override {}
63-
void visit (AST::LazyBooleanExpr &expr) override {}
64-
void visit (AST::TypeCastExpr &expr) override {}
65-
void visit (AST::AssignmentExpr &expr) override {}
66-
void visit (AST::CompoundAssignmentExpr &expr) override {}
67-
void visit (AST::GroupedExpr &expr) override {}
68-
void visit (AST::ArrayElemsValues &elems) override {}
69-
void visit (AST::ArrayElemsCopied &elems) override {}
70-
void visit (AST::ArrayExpr &expr) override {}
71-
void visit (AST::ArrayIndexExpr &expr) override {}
72-
void visit (AST::TupleExpr &expr) override {}
73-
void visit (AST::TupleIndexExpr &expr) override {}
74-
void visit (AST::StructExprStruct &expr) override {}
75-
void visit (AST::StructExprFieldIdentifier &field) override {}
76-
void visit (AST::StructExprFieldIdentifierValue &field) override {}
77-
void visit (AST::StructExprFieldIndexValue &field) override {}
78-
void visit (AST::StructExprStructFields &expr) override {}
79-
void visit (AST::StructExprStructBase &expr) override {}
80-
void visit (AST::CallExpr &expr) override {}
81-
void visit (AST::MethodCallExpr &expr) override {}
82-
void visit (AST::FieldAccessExpr &expr) override {}
83-
void visit (AST::ClosureExprInner &expr) override {}
84-
void visit (AST::ClosureExprInnerTyped &expr) override {}
85-
void visit (AST::ContinueExpr &expr) override {}
86-
void visit (AST::BreakExpr &expr) override {}
87-
void visit (AST::RangeFromToExpr &expr) override {}
88-
void visit (AST::RangeFromExpr &expr) override {}
89-
void visit (AST::RangeToExpr &expr) override {}
90-
void visit (AST::RangeFullExpr &expr) override {}
91-
void visit (AST::RangeFromToInclExpr &expr) override {}
92-
void visit (AST::RangeToInclExpr &expr) override {}
93-
void visit (AST::ReturnExpr &expr) override {}
9440
void visit (AST::BoxExpr &expr) override;
95-
void visit (AST::UnsafeBlockExpr &expr) override {}
96-
void visit (AST::LoopExpr &expr) override {}
97-
void visit (AST::WhileLoopExpr &expr) override {}
98-
void visit (AST::WhileLetLoopExpr &expr) override {}
99-
void visit (AST::ForLoopExpr &expr) override {}
100-
void visit (AST::IfExpr &expr) override {}
101-
void visit (AST::IfExprConseqElse &expr) override {}
102-
void visit (AST::IfLetExprConseqElse &expr) override {}
103-
void visit (AST::AwaitExpr &expr) override {}
104-
void visit (AST::AsyncBlockExpr &expr) override {}
10541
void visit (AST::TypeParam &param) override;
106-
void visit (AST::LifetimeWhereClauseItem &item) override {}
107-
void visit (AST::TypeBoundWhereClauseItem &item) override {}
108-
void visit (AST::Module &module) override {}
109-
void visit (AST::ExternCrate &crate) override {}
11042
void visit (AST::UseTreeGlob &use_tree) override;
111-
void visit (AST::UseTreeList &use_tree) override {}
112-
void visit (AST::UseTreeRebind &use_tree) override {}
113-
void visit (AST::UseDeclaration &use_decl) override {}
11443
void visit (AST::Function &function) override;
115-
void visit (AST::TypeAlias &type_alias) override {}
116-
void visit (AST::StructStruct &struct_item) override {}
117-
void visit (AST::TupleStruct &tuple_struct) override {}
118-
void visit (AST::EnumItem &item) override {}
119-
void visit (AST::EnumItemTuple &item) override {}
120-
void visit (AST::EnumItemStruct &item) override {}
121-
void visit (AST::EnumItemDiscriminant &item) override {}
122-
void visit (AST::Enum &enum_item) override {}
123-
void visit (AST::Union &union_item) override {}
124-
void visit (AST::ConstantItem &const_item) override {}
125-
void visit (AST::StaticItem &static_item) override {}
126-
void visit (AST::TraitItemConst &item) override {}
127-
void visit (AST::TraitItemType &item) override {}
12844
void visit (AST::TraitImpl &impl) override;
129-
void visit (AST::Trait &trait) override {}
13045
void visit (AST::ExternalTypeItem &item) override;
131-
void visit (AST::ExternalStaticItem &item) override {}
13246
void visit (AST::ExternBlock &block) override;
133-
void visit (AST::MacroMatchFragment &match) override {}
134-
void visit (AST::MacroMatchRepetition &match) override {}
135-
void visit (AST::MacroMatcher &matcher) override {}
13647
void visit (AST::MacroRulesDefinition &rules_def) override;
137-
void visit (AST::MacroInvocation &macro_invoc) override {}
138-
void visit (AST::MetaItemPath &meta_item) override {}
139-
void visit (AST::MetaItemSeq &meta_item) override {}
140-
void visit (AST::MetaWord &meta_item) override {}
141-
void visit (AST::MetaNameValueStr &meta_item) override {}
142-
void visit (AST::MetaListPaths &meta_item) override {}
143-
void visit (AST::MetaListNameValueStr &meta_item) override {}
144-
void visit (AST::LiteralPattern &pattern) override {}
145-
void visit (AST::IdentifierPattern &pattern) override {}
146-
void visit (AST::WildcardPattern &pattern) override {}
147-
void visit (AST::RestPattern &pattern) override {}
148-
void visit (AST::RangePatternBoundLiteral &bound) override {}
149-
void visit (AST::RangePatternBoundPath &bound) override {}
150-
void visit (AST::RangePatternBoundQualPath &bound) override {}
15148
void visit (AST::RangePattern &pattern) override;
152-
void visit (AST::ReferencePattern &pattern) override {}
153-
void visit (AST::StructPatternFieldTuplePat &field) override {}
154-
void visit (AST::StructPatternFieldIdentPat &field) override {}
155-
void visit (AST::StructPatternFieldIdent &field) override {}
156-
void visit (AST::StructPattern &pattern) override {}
157-
void visit (AST::TupleStructItemsNoRange &tuple_items) override {}
158-
void visit (AST::TupleStructItemsRange &tuple_items) override {}
159-
void visit (AST::TupleStructPattern &pattern) override {}
160-
void visit (AST::TuplePatternItemsMultiple &tuple_items) override {}
161-
void visit (AST::TuplePatternItemsRanged &tuple_items) override {}
162-
void visit (AST::TuplePattern &pattern) override {}
163-
void visit (AST::GroupedPattern &pattern) override {}
164-
void visit (AST::SlicePattern &pattern) override {}
165-
void visit (AST::AltPattern &pattern) override {}
166-
void visit (AST::EmptyStmt &stmt) override {}
167-
void visit (AST::ExprStmt &stmt) override {}
168-
void visit (AST::TraitBound &bound) override {}
169-
void visit (AST::ImplTraitType &type) override {}
170-
void visit (AST::TraitObjectType &type) override {}
171-
void visit (AST::ParenthesisedType &type) override {}
172-
void visit (AST::ImplTraitTypeOneBound &type) override {}
173-
void visit (AST::TraitObjectTypeOneBound &type) override {}
174-
void visit (AST::TupleType &type) override {}
175-
void visit (AST::NeverType &type) override {}
176-
void visit (AST::RawPointerType &type) override {}
177-
void visit (AST::ReferenceType &type) override {}
178-
void visit (AST::ArrayType &type) override {}
179-
void visit (AST::SliceType &type) override {}
180-
void visit (AST::InferredType &type) override {}
181-
void visit (AST::BareFunctionType &type) override {}
182-
void visit (AST::FunctionParam &param) override {}
183-
void visit (AST::VariadicParam &param) override {}
184-
void visit (AST::SelfParam &param) override {}
18549

18650
private:
18751
void gate (Feature::Name name, location_t loc, const std::string &error_msg);

gcc/rust/checks/errors/rust-feature.cc

+17-23
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,44 @@
1717
// <http://www.gnu.org/licenses/>.
1818

1919
#include "rust-feature.h"
20-
#include "rust-session-manager.h"
2120

2221
namespace Rust {
2322

2423
Feature
25-
Feature::create (Feature::Name name)
24+
Feature::create (Feature::Name f)
2625
{
27-
switch (name)
26+
switch (f)
2827
{
2928
case Feature::Name::ASSOCIATED_TYPE_BOUNDS:
3029
return Feature (Feature::Name::ASSOCIATED_TYPE_BOUNDS,
3130
Feature::State::ACCEPTED, "associated_type_bounds",
32-
"1.34.0", 52662, tl::nullopt, "");
31+
"1.34.0", 52662);
3332
case Feature::Name::INTRINSICS:
34-
return Feature (Feature::Name::INTRINSICS, Feature::State::ACCEPTED,
35-
"intrinsics", "1.0.0", 0, tl::nullopt, "");
33+
return Feature (f, Feature::State::ACCEPTED, "intrinsics", "1.0.0");
3634
case Feature::Name::RUSTC_ATTRS:
37-
return Feature (Feature::Name::RUSTC_ATTRS, Feature::State::ACCEPTED,
38-
"rustc_attrs", "1.0.0", 0, tl::nullopt, "");
35+
return Feature (f, Feature::State::ACCEPTED, "rustc_attrs", "1.0.0");
3936
case Feature::Name::DECL_MACRO:
40-
return Feature (Feature::Name::DECL_MACRO, Feature::State::ACCEPTED,
41-
"decl_macro", "1.0.0", 0, tl::nullopt, "");
37+
return Feature (f, Feature::State::ACCEPTED, "decl_macro", "1.0.0",
38+
39412);
4239
case Feature::Name::EXTERN_TYPES:
43-
return Feature (Feature::Name::EXTERN_TYPES, Feature::State::ACTIVE,
44-
"extern_types", "1.23.0", 43467, tl::nullopt, "");
40+
return Feature (f, Feature::State::ACTIVE, "extern_types", "1.23.0",
41+
43467);
4542
case Feature::Name::NEGATIVE_IMPLS:
46-
return Feature (Feature::Name::NEGATIVE_IMPLS, Feature::State::ACTIVE,
47-
"negative_impls", "1.0.0", 68318, tl::nullopt, "");
43+
return Feature (f, Feature::State::ACTIVE, "negative_impls", "1.0.0",
44+
68318);
4845
case Feature::Name::BOX_SYNTAX:
49-
return Feature (Feature::Name::BOX_SYNTAX, Feature::State::ACTIVE,
50-
"box_syntax", "1.0.0", 49733, tl::nullopt, "");
46+
return Feature (f, Feature::State::ACTIVE, "box_syntax", "1.0.0", 49733);
5147
case Feature::Name::DROPCK_EYEPATCH:
52-
return Feature (Feature::Name::DROPCK_EYEPATCH, Feature::State::ACTIVE,
53-
"dropck_eyepatch", "1.10.0", 34761, tl::nullopt, "");
48+
return Feature (f, Feature::State::ACTIVE, "dropck_eyepatch", "1.10.0",
49+
34761);
5450
case Feature::Name::RAW_REF_OP:
55-
return Feature (Feature::Name::RAW_REF_OP, Feature::State::ACTIVE,
56-
"raw_ref_op", "1.41.0", 64490, tl::nullopt, "");
51+
return Feature (f, Feature::State::ACTIVE, "raw_ref_op", "1.41.0", 64490);
5752
case Feature::Name::EXCLUSIVE_RANGE_PATTERN:
5853
return Feature (Feature::Name::EXCLUSIVE_RANGE_PATTERN,
5954
Feature::State::ACTIVE, "exclusive_range_pattern",
60-
"1.11.0", 37854, tl::nullopt, "");
55+
"1.11.0", 37854);
6156
case Feature::Name::PRELUDE_IMPORT:
62-
return Feature (Feature::Name::PRELUDE_IMPORT, Feature::State::ACTIVE,
63-
"prelude_import", "1.0.0", 0, tl::nullopt, "");
57+
return Feature (f, Feature::State::ACTIVE, "prelude_import", "1.0.0");
6458
default:
6559
rust_unreachable ();
6660
}

gcc/rust/checks/errors/rust-feature.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ class Feature
5757
Name name () { return m_name; }
5858
const std::string &description () { return m_description; }
5959
State state () { return m_state; }
60-
unsigned issue () { return m_issue; }
60+
tl::optional<unsigned> issue () { return m_issue; }
6161

6262
static tl::optional<Name> as_name (const std::string &name);
6363
static Feature create (Name name);
6464

6565
private:
6666
Feature (Name name, State state, const char *name_str,
67-
const char *rustc_since, unsigned issue_number,
68-
const tl::optional<CompileOptions::Edition> &edition,
69-
const char *description)
67+
const char *rustc_since,
68+
tl::optional<unsigned> issue_number = tl::nullopt,
69+
const tl::optional<CompileOptions::Edition> &edition = tl::nullopt,
70+
const char *description = "")
7071
: m_state (state), m_name (name), m_name_str (name_str),
7172
m_rustc_since (rustc_since), m_issue (issue_number), edition (edition),
7273
m_description (description)
@@ -76,9 +77,9 @@ class Feature
7677
Name m_name;
7778
std::string m_name_str;
7879
std::string m_rustc_since;
79-
unsigned m_issue;
80+
tl::optional<unsigned> m_issue;
8081
tl::optional<CompileOptions::Edition> edition;
81-
std::string m_description;
82+
std::string m_description; // TODO: Switch to optional?
8283

8384
static const std::map<std::string, Name> name_hash_map;
8485
};

gcc/testsuite/rust/compile/assume.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(intrinsics)]
2+
13
mod intrinsics {
24
extern "rust-intrinsic" {
35
pub fn assume(value: bool);

gcc/testsuite/rust/compile/issue-1901.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(intrinsics)]
2+
13
#[lang = "sized"]
24
pub trait Sized {}
35

gcc/testsuite/rust/compile/issue-1981.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(intrinsics)]
2+
13
#[lang = "sized"]
24
pub trait Sized {}
35

gcc/testsuite/rust/compile/sizeof-stray-infer-var-bug.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(intrinsics)]
2+
13
#[lang = "sized"]
24
pub trait Sized {}
35

gcc/testsuite/rust/compile/torture/intrinsics-8.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(intrinsics)]
2+
13
#[lang = "sized"]
24
pub trait Sized {}
35

gcc/testsuite/rust/compile/torture/transmute-size-check-1.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(intrinsics)]
2+
13
#[lang = "sized"]
24
pub trait Sized {}
35

gcc/testsuite/rust/compile/torture/transmute1.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(intrinsics)]
2+
13
#[lang = "sized"]
24
pub trait Sized {}
35

gcc/testsuite/rust/compile/torture/uninit-intrinsic-1.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(intrinsics)]
2+
13
#[lang = "sized"]
24
pub trait Sized {}
35

gcc/testsuite/rust/execute/torture/issue-1436.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// { dg-options "-w" }
2+
3+
#![feature(intrinsics)]
4+
25
#[lang = "sized"]
36
pub trait Sized {}
47

0 commit comments

Comments
 (0)