Skip to content

Commit

Permalink
feature-gate: Cleanup visitor and constructor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CohenArthur committed Aug 22, 2024
1 parent 06e8b82 commit 084789e
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 169 deletions.
39 changes: 39 additions & 0 deletions gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions gcc/rust/checks/errors/rust-feature-gate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "rust-attribute-values.h"
#include "rust-ast-visitor.h"
#include "rust-feature.h"
#include "rust-ast-full.h"

namespace Rust {

Expand Down Expand Up @@ -75,16 +76,17 @@ FeatureGate::gate (Feature::Name name, location_t loc,
if (!valid_features.count (name))
{
auto feature = Feature::create (name);
auto issue = feature.issue ();
if (issue > 0)
if (auto issue = feature.issue ())
{
auto issue_number = issue.value ();
const char *fmt_str
= "%s. see issue %u "
"<https://github.com/rust-lang/rust/issues/%u> for more "
"information. add `#![feature(%s)]` to the crate attributes to "
"enable.";
rust_error_at (loc, ErrorCode::E0658, fmt_str, error_msg.c_str (),
issue, issue, feature.as_string ().c_str ());
issue_number, issue_number,
feature.as_string ().c_str ());
}
else
{
Expand Down Expand Up @@ -169,7 +171,7 @@ FeatureGate::visit (AST::TraitImpl &impl)
"negative_impls are not yet implemented");

AST::DefaultASTVisitor::visit (impl);
};
}

void
FeatureGate::visit (AST::BoxExpr &expr)
Expand Down
136 changes: 0 additions & 136 deletions gcc/rust/checks/errors/rust-feature-gate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define RUST_FEATURE_GATE_H

#include "rust-ast-visitor.h"
#include "rust-ast-full.h"
#include "rust-feature.h"

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

void visit (AST::Token &tok) override {}
void visit (AST::DelimTokenTree &delim_tok_tree) override {}
void visit (AST::AttrInputMetaItemContainer &input) override {}
void visit (AST::IdentifierExpr &ident_expr) override {}
void visit (AST::Lifetime &lifetime) override {}
void visit (AST::LifetimeParam &lifetime_param) override;
void visit (AST::ConstGenericParam &const_param) override;
void visit (AST::PathInExpression &path) override {}
void visit (AST::TypePathSegment &segment) override {}
void visit (AST::TypePathSegmentGeneric &segment) override {}
void visit (AST::TypePathSegmentFunction &segment) override {}
void visit (AST::TypePath &path) override {}
void visit (AST::QualifiedPathInExpression &path) override {}
void visit (AST::QualifiedPathInType &path) override {}
void visit (AST::LiteralExpr &expr) override {}
void visit (AST::AttrInputLiteral &attr_input) override {}
void visit (AST::AttrInputMacro &attr_input) override {}
void visit (AST::MetaItemLitExpr &meta_item) override {}
void visit (AST::MetaItemPathLit &meta_item) override {}
void visit (AST::BorrowExpr &expr) override;
void visit (AST::DereferenceExpr &expr) override {}
void visit (AST::ErrorPropagationExpr &expr) override {}
void visit (AST::NegationExpr &expr) override {}
void visit (AST::ArithmeticOrLogicalExpr &expr) override {}
void visit (AST::ComparisonExpr &expr) override {}
void visit (AST::LazyBooleanExpr &expr) override {}
void visit (AST::TypeCastExpr &expr) override {}
void visit (AST::AssignmentExpr &expr) override {}
void visit (AST::CompoundAssignmentExpr &expr) override {}
void visit (AST::GroupedExpr &expr) override {}
void visit (AST::ArrayElemsValues &elems) override {}
void visit (AST::ArrayElemsCopied &elems) override {}
void visit (AST::ArrayExpr &expr) override {}
void visit (AST::ArrayIndexExpr &expr) override {}
void visit (AST::TupleExpr &expr) override {}
void visit (AST::TupleIndexExpr &expr) override {}
void visit (AST::StructExprStruct &expr) override {}
void visit (AST::StructExprFieldIdentifier &field) override {}
void visit (AST::StructExprFieldIdentifierValue &field) override {}
void visit (AST::StructExprFieldIndexValue &field) override {}
void visit (AST::StructExprStructFields &expr) override {}
void visit (AST::StructExprStructBase &expr) override {}
void visit (AST::CallExpr &expr) override {}
void visit (AST::MethodCallExpr &expr) override {}
void visit (AST::FieldAccessExpr &expr) override {}
void visit (AST::ClosureExprInner &expr) override {}
void visit (AST::ClosureExprInnerTyped &expr) override {}
void visit (AST::ContinueExpr &expr) override {}
void visit (AST::BreakExpr &expr) override {}
void visit (AST::RangeFromToExpr &expr) override {}
void visit (AST::RangeFromExpr &expr) override {}
void visit (AST::RangeToExpr &expr) override {}
void visit (AST::RangeFullExpr &expr) override {}
void visit (AST::RangeFromToInclExpr &expr) override {}
void visit (AST::RangeToInclExpr &expr) override {}
void visit (AST::ReturnExpr &expr) override {}
void visit (AST::BoxExpr &expr) override;
void visit (AST::UnsafeBlockExpr &expr) override {}
void visit (AST::LoopExpr &expr) override {}
void visit (AST::WhileLoopExpr &expr) override {}
void visit (AST::WhileLetLoopExpr &expr) override {}
void visit (AST::ForLoopExpr &expr) override {}
void visit (AST::IfExpr &expr) override {}
void visit (AST::IfExprConseqElse &expr) override {}
void visit (AST::IfLetExprConseqElse &expr) override {}
void visit (AST::AwaitExpr &expr) override {}
void visit (AST::AsyncBlockExpr &expr) override {}
void visit (AST::TypeParam &param) override;
void visit (AST::LifetimeWhereClauseItem &item) override {}
void visit (AST::TypeBoundWhereClauseItem &item) override {}
void visit (AST::Module &module) override {}
void visit (AST::ExternCrate &crate) override {}
void visit (AST::UseTreeGlob &use_tree) override;
void visit (AST::UseTreeList &use_tree) override {}
void visit (AST::UseTreeRebind &use_tree) override {}
void visit (AST::UseDeclaration &use_decl) override {}
void visit (AST::Function &function) override;
void visit (AST::TypeAlias &type_alias) override {}
void visit (AST::StructStruct &struct_item) override {}
void visit (AST::TupleStruct &tuple_struct) override {}
void visit (AST::EnumItem &item) override {}
void visit (AST::EnumItemTuple &item) override {}
void visit (AST::EnumItemStruct &item) override {}
void visit (AST::EnumItemDiscriminant &item) override {}
void visit (AST::Enum &enum_item) override {}
void visit (AST::Union &union_item) override {}
void visit (AST::ConstantItem &const_item) override {}
void visit (AST::StaticItem &static_item) override {}
void visit (AST::TraitItemConst &item) override {}
void visit (AST::TraitItemType &item) override {}
void visit (AST::TraitImpl &impl) override;
void visit (AST::Trait &trait) override {}
void visit (AST::ExternalTypeItem &item) override;
void visit (AST::ExternalStaticItem &item) override {}
void visit (AST::ExternBlock &block) override;
void visit (AST::MacroMatchFragment &match) override {}
void visit (AST::MacroMatchRepetition &match) override {}
void visit (AST::MacroMatcher &matcher) override {}
void visit (AST::MacroRulesDefinition &rules_def) override;
void visit (AST::MacroInvocation &macro_invoc) override {}
void visit (AST::MetaItemPath &meta_item) override {}
void visit (AST::MetaItemSeq &meta_item) override {}
void visit (AST::MetaWord &meta_item) override {}
void visit (AST::MetaNameValueStr &meta_item) override {}
void visit (AST::MetaListPaths &meta_item) override {}
void visit (AST::MetaListNameValueStr &meta_item) override {}
void visit (AST::LiteralPattern &pattern) override {}
void visit (AST::IdentifierPattern &pattern) override {}
void visit (AST::WildcardPattern &pattern) override {}
void visit (AST::RestPattern &pattern) override {}
void visit (AST::RangePatternBoundLiteral &bound) override {}
void visit (AST::RangePatternBoundPath &bound) override {}
void visit (AST::RangePatternBoundQualPath &bound) override {}
void visit (AST::RangePattern &pattern) override;
void visit (AST::ReferencePattern &pattern) override {}
void visit (AST::StructPatternFieldTuplePat &field) override {}
void visit (AST::StructPatternFieldIdentPat &field) override {}
void visit (AST::StructPatternFieldIdent &field) override {}
void visit (AST::StructPattern &pattern) override {}
void visit (AST::TupleStructItemsNoRange &tuple_items) override {}
void visit (AST::TupleStructItemsRange &tuple_items) override {}
void visit (AST::TupleStructPattern &pattern) override {}
void visit (AST::TuplePatternItemsMultiple &tuple_items) override {}
void visit (AST::TuplePatternItemsRanged &tuple_items) override {}
void visit (AST::TuplePattern &pattern) override {}
void visit (AST::GroupedPattern &pattern) override {}
void visit (AST::SlicePattern &pattern) override {}
void visit (AST::AltPattern &pattern) override {}
void visit (AST::EmptyStmt &stmt) override {}
void visit (AST::ExprStmt &stmt) override {}
void visit (AST::TraitBound &bound) override {}
void visit (AST::ImplTraitType &type) override {}
void visit (AST::TraitObjectType &type) override {}
void visit (AST::ParenthesisedType &type) override {}
void visit (AST::ImplTraitTypeOneBound &type) override {}
void visit (AST::TraitObjectTypeOneBound &type) override {}
void visit (AST::TupleType &type) override {}
void visit (AST::NeverType &type) override {}
void visit (AST::RawPointerType &type) override {}
void visit (AST::ReferenceType &type) override {}
void visit (AST::ArrayType &type) override {}
void visit (AST::SliceType &type) override {}
void visit (AST::InferredType &type) override {}
void visit (AST::BareFunctionType &type) override {}
void visit (AST::FunctionParam &param) override {}
void visit (AST::VariadicParam &param) override {}
void visit (AST::SelfParam &param) override {}

private:
void gate (Feature::Name name, location_t loc, const std::string &error_msg);
Expand Down
40 changes: 17 additions & 23 deletions gcc/rust/checks/errors/rust-feature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,44 @@
// <http://www.gnu.org/licenses/>.

#include "rust-feature.h"
#include "rust-session-manager.h"

namespace Rust {

Feature
Feature::create (Feature::Name name)
Feature::create (Feature::Name f)
{
switch (name)
switch (f)
{
case Feature::Name::ASSOCIATED_TYPE_BOUNDS:
return Feature (Feature::Name::ASSOCIATED_TYPE_BOUNDS,
Feature::State::ACCEPTED, "associated_type_bounds",
"1.34.0", 52662, tl::nullopt, "");
"1.34.0", 52662);
case Feature::Name::INTRINSICS:
return Feature (Feature::Name::INTRINSICS, Feature::State::ACCEPTED,
"intrinsics", "1.0.0", 0, tl::nullopt, "");
return Feature (f, Feature::State::ACCEPTED, "intrinsics", "1.0.0");
case Feature::Name::RUSTC_ATTRS:
return Feature (Feature::Name::RUSTC_ATTRS, Feature::State::ACCEPTED,
"rustc_attrs", "1.0.0", 0, tl::nullopt, "");
return Feature (f, Feature::State::ACCEPTED, "rustc_attrs", "1.0.0");
case Feature::Name::DECL_MACRO:
return Feature (Feature::Name::DECL_MACRO, Feature::State::ACCEPTED,
"decl_macro", "1.0.0", 0, tl::nullopt, "");
return Feature (f, Feature::State::ACCEPTED, "decl_macro", "1.0.0",
39412);
case Feature::Name::EXTERN_TYPES:
return Feature (Feature::Name::EXTERN_TYPES, Feature::State::ACTIVE,
"extern_types", "1.23.0", 43467, tl::nullopt, "");
return Feature (f, Feature::State::ACTIVE, "extern_types", "1.23.0",
43467);
case Feature::Name::NEGATIVE_IMPLS:
return Feature (Feature::Name::NEGATIVE_IMPLS, Feature::State::ACTIVE,
"negative_impls", "1.0.0", 68318, tl::nullopt, "");
return Feature (f, Feature::State::ACTIVE, "negative_impls", "1.0.0",
68318);
case Feature::Name::BOX_SYNTAX:
return Feature (Feature::Name::BOX_SYNTAX, Feature::State::ACTIVE,
"box_syntax", "1.0.0", 49733, tl::nullopt, "");
return Feature (f, Feature::State::ACTIVE, "box_syntax", "1.0.0", 49733);
case Feature::Name::DROPCK_EYEPATCH:
return Feature (Feature::Name::DROPCK_EYEPATCH, Feature::State::ACTIVE,
"dropck_eyepatch", "1.10.0", 34761, tl::nullopt, "");
return Feature (f, Feature::State::ACTIVE, "dropck_eyepatch", "1.10.0",
34761);
case Feature::Name::RAW_REF_OP:
return Feature (Feature::Name::RAW_REF_OP, Feature::State::ACTIVE,
"raw_ref_op", "1.41.0", 64490, tl::nullopt, "");
return Feature (f, Feature::State::ACTIVE, "raw_ref_op", "1.41.0", 64490);
case Feature::Name::EXCLUSIVE_RANGE_PATTERN:
return Feature (Feature::Name::EXCLUSIVE_RANGE_PATTERN,
Feature::State::ACTIVE, "exclusive_range_pattern",
"1.11.0", 37854, tl::nullopt, "");
"1.11.0", 37854);
case Feature::Name::PRELUDE_IMPORT:
return Feature (Feature::Name::PRELUDE_IMPORT, Feature::State::ACTIVE,
"prelude_import", "1.0.0", 0, tl::nullopt, "");
return Feature (f, Feature::State::ACTIVE, "prelude_import", "1.0.0");
default:
rust_unreachable ();
}
Expand Down
13 changes: 7 additions & 6 deletions gcc/rust/checks/errors/rust-feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ class Feature
Name name () { return m_name; }
const std::string &description () { return m_description; }
State state () { return m_state; }
unsigned issue () { return m_issue; }
tl::optional<unsigned> issue () { return m_issue; }

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

private:
Feature (Name name, State state, const char *name_str,
const char *rustc_since, unsigned issue_number,
const tl::optional<CompileOptions::Edition> &edition,
const char *description)
const char *rustc_since,
tl::optional<unsigned> issue_number = tl::nullopt,
const tl::optional<CompileOptions::Edition> &edition = tl::nullopt,
const char *description = "")
: m_state (state), m_name (name), m_name_str (name_str),
m_rustc_since (rustc_since), m_issue (issue_number), edition (edition),
m_description (description)
Expand All @@ -76,9 +77,9 @@ class Feature
Name m_name;
std::string m_name_str;
std::string m_rustc_since;
unsigned m_issue;
tl::optional<unsigned> m_issue;
tl::optional<CompileOptions::Edition> edition;
std::string m_description;
std::string m_description; // TODO: Switch to optional?

static const std::map<std::string, Name> name_hash_map;
};
Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/assume.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(intrinsics)]

mod intrinsics {
extern "rust-intrinsic" {
pub fn assume(value: bool);
Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/issue-1901.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/issue-1981.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/sizeof-stray-infer-var-bug.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/torture/intrinsics-8.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/torture/transmute-size-check-1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/torture/transmute1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/torture/uninit-intrinsic-1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

Expand Down
3 changes: 3 additions & 0 deletions gcc/testsuite/rust/execute/torture/issue-1436.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// { dg-options "-w" }

#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

Expand Down
Loading

0 comments on commit 084789e

Please sign in to comment.