Skip to content

Commit

Permalink
Emit an error on associated const without values
Browse files Browse the repository at this point in the history
Associated const with no value that are not in trait impl are prohibited.

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::check): Launch
	check over the whole given crate.
	(ASTValidation::visit): Implement visitor for some members of the ast.
	* checks/errors/rust-ast-validation.h: Update some prototype according
	to implemented visitor functions. Also add a context tracker.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
  • Loading branch information
P-E-P authored and CohenArthur committed Nov 6, 2023
1 parent 230cd6d commit fb346fa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
33 changes: 32 additions & 1 deletion gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.

#include "rust-ast-validation.h"
#include "rust-diagnostics.h"

namespace Rust {

Expand All @@ -37,14 +38,44 @@ ASTValidation::visit (std::unique_ptr<T> &node)
void
ASTValidation::check (AST::Crate &crate)
{
push_context (Context::CRATE);
for (auto &item : crate.items)
{
visit (item);
}
pop_context ();
}

void
ASTValidation::visit (AST::InherentImpl &impl)
{}
{
push_context (Context::INHERENT_IMPL);
for (auto &item : impl.get_impl_items ())
{
visit (item);
}
pop_context ();
}

void
ASTValidation::visit (AST::TraitImpl &impl)
{
push_context (Context::TRAIT_IMPL);
for (auto &item : impl.get_impl_items ())
{
visit (item);
}
pop_context ();
}

void
ASTValidation::visit (AST::ConstantItem &const_item)
{
if (!const_item.has_expr () && context.back () != Context::TRAIT_IMPL)
{
rust_error_at (const_item.get_locus (),
"associated constant in %<impl%> without body");
}
}

} // namespace Rust
18 changes: 16 additions & 2 deletions gcc/rust/checks/errors/rust-ast-validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ class ASTValidation : public AST::ASTVisitor
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::ConstantItem &const_item) override;
void visit (AST::StaticItem &static_item) override {}
void visit (AST::TraitItemFunc &item) override {}
void visit (AST::TraitItemMethod &item) override {}
void visit (AST::TraitItemConst &item) override {}
void visit (AST::TraitItemType &item) override {}
void visit (AST::Trait &trait) override {}
void visit (AST::InherentImpl &impl) override;
void visit (AST::TraitImpl &impl) override {}
void visit (AST::TraitImpl &impl) override;
void visit (AST::ExternalTypeItem &item) override {}
void visit (AST::ExternalStaticItem &item) override {}
void visit (AST::ExternalFunctionItem &item) override {}
Expand Down Expand Up @@ -187,6 +187,20 @@ class ASTValidation : public AST::ASTVisitor
void visit (AST::SliceType &type) override {}
void visit (AST::InferredType &type) override {}
void visit (AST::BareFunctionType &type) override {}

private:
enum class Context
{
INHERENT_IMPL,
TRAIT_IMPL,
CRATE,
};

std::vector<Context> context;

void push_context (Context ctx) { context.push_back (ctx); }

void pop_context () { context.pop_back (); }
};

} // namespace Rust
Expand Down

0 comments on commit fb346fa

Please sign in to comment.