There was a mismatch whether lifetime 'static is parsed as "static"
or "'static".
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::lifetime_from_token): Fix matched pattern.
Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
gcc/rust/ChangeLog:
* hir/tree/rust-hir-item.h: Ad lifetime getter.
* hir/tree/rust-hir-path.h: Make getter const ref.
* hir/tree/rust-hir.h: Const ref and new getter.
Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
Many visit functions in the expand visitor simply visit their components
like the default visitor. Making the expand visitor inherit from the
default visitor allows us to keep all visitor in sync without having to
change every visitor.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc (ExpandVisitor::go): Add call to visit
on the crate.
(ExpandVisitor::visit): Remove some visit functions in favor of their
default visitor counterpart.
* expand/rust-expand-visitor.h (class ExpandVisitor): Inherit from
default visitor and remove now useless function prototypes.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
The default resolver put some scope in place but mostly has traversal
functions similar to the default ast visitor, making it inherit from the
default visitor allows us to avoid code duplication.
gcc/rust/ChangeLog:
* resolve/rust-default-resolver.cc (DefaultResolver::visit): Remove
duplicated functions.
* resolve/rust-default-resolver.h (class DefaultResolver): Make the
default resolver inherit from the default visitor.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fixes issue #2040
Add check to assure that a function cant be declared const inside trait impl
blocks.
gcc/rust/ChangeLog:
* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
check for const funtion.
gcc/testsuite/ChangeLog:
* rust/compile/issue-2040.rs: New test.
Signed-off-by: Nobel Singh <nobel2073@gmail.com>
Function body check emit an error message when a required function body
is missing.
gcc/testsuite/ChangeLog:
* rust/compile/functions_without_body.rs: New test.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add checks in the ast validation pass to error out with functions
(either free or associated) without a definition.
gcc/rust/ChangeLog:
* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
a validation check and emit an error depending on the context.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Missing body on a function should be rejected at a later stage in the
compiler, not during parsing.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::visit): Adapt defintion
getter.
* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise.
* expand/rust-cfg-strip.cc (CfgStrip::visit): Likewise.
* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise.
* hir/rust-ast-lower-implitem.h: Likewise.
* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise.
* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Likewise.
* resolve/rust-ast-resolve-stmt.h: Likewise.
* resolve/rust-default-resolver.cc (DefaultResolver::visit): Likewise.
* util/rust-attributes.cc (AttributeChecker::visit): Likewise.
* parse/rust-parse-impl.h: Allow empty function body during parsing.
* ast/rust-ast.cc (Function::Function): Constructor now take an
optional for the body.
(Function::operator=): Adapt to new optional member.
(Function::as_string): Likewise.
* ast/rust-item.h (class Function): Make body optional and do not
rely on nullptr anymore.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
We already have some compile tests but it lacked an execution test to
ensure everything compiles correctly to the correct value.
gcc/testsuite/ChangeLog:
* rust/execute/torture/name_resolution.rs: New test.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Local variables and functions or global variables have different
resolution when binded to a variable. This was not covered before, even
though it was handled by the new name resolution. This commit highlight
this behavior from the new name resolution mechanism.
gcc/testsuite/ChangeLog:
* rust/compile/name_resolution11.rs: New test.
* rust/compile/name_resolution12.rs: New test.
* rust/compile/name_resolution13.rs: New test.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Many visit functions in the early name resolver are just plain old
traversal visit functions like the ones from the default visitor.
gcc/rust/ChangeLog:
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::resolve_generic_args):
Move function.
(EarlyNameResolver::resolve_qualified_path_type): Likewise.
(EarlyNameResolver::visit): Add a top level visit function for crate
and remove duplicated code.
* resolve/rust-early-name-resolver.h (class EarlyNameResolver): Update
overriden function list.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Make the attribute checker visitor inherit from the default visitor
in order to keep visit behavior shared.
gcc/rust/ChangeLog:
* util/rust-attributes.cc (AttributeChecker::visit): Add visit function
for crates.
* util/rust-attributes.h (class AttributeChecker): Update function
prototypes.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
The feature gating behavior may be shortened and kept cleaner using the
default visitor. This means less maintenance on visit functions as the
traversal is shared by multiple visitors.
gcc/rust/ChangeLog:
* checks/errors/rust-feature-gate.cc (FeatureGate::visit): Add a visit
function for the crate level.
(FeatureGate::check): Add call to crate visit.
* checks/errors/rust-feature-gate.h (class FeatureGate): Remove now
useless visit functions (traversal only).
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Const fn declaration in trait declarations shall emit an error. This new
test highlight this behavior.
gcc/testsuite/ChangeLog:
* rust/compile/const_trait_fn.rs: New test.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add a new check in AST validation pass that checks that no function
declaration in traits are declared const.
gcc/rust/ChangeLog:
* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
const check.
* checks/errors/rust-ast-validation.h: Add visit function prototype.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Functions that are both async and const shall be rejected during the
AST validation pass. This new test highlight this behavior.
gcc/testsuite/ChangeLog:
* rust/compile/const_async_function.rs: New test.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Add a check during AST validation pass to ensure functions are either
const or async but not both.
gcc/rust/ChangeLog:
* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
async const check.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
We need to account for const specifiers in async parsing as const
can be used in the syntax before the async keyword.
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::parse_vis_item): Allow parsing async
items in const.
(Parser::parse_async_item): Account for const offset during async
lookahead.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
A function cannot be both async and const, however this should not be
handled in the parser but rather at a later stage in the compiler. This
commit change the AsyncConstStatus in the AST and the HIR to allows a
function to be both async and const.
gcc/rust/ChangeLog:
* ast/rust-ast-builder.cc (AstBuilder::fn_qualifiers): Change
constructor to match the new arguments.
* ast/rust-ast-collector.cc (TokenCollector::visit): Change behavior
to handle both const and async specifiers at the same time.
* ast/rust-ast.cc (FunctionQualifiers::as_string): Likewise.
* ast/rust-item.h (class FunctionQualifiers): Remove AsyncConstStatus
and replace it with both Async and Const status. Also change the safety
arguments to use an enum instead of a boolean.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_qualifiers):
Update constructor call.
* hir/tree/rust-hir-item.h: Add Const and Async status, remove
AsyncConstStatus, update the constructor.
* hir/tree/rust-hir.cc (FunctionQualifiers::as_string): Update with
the new status.
* parse/rust-parse-impl.h (Parser::parse_function_qualifiers): Update
constructor call.
* util/rust-common.h (enum Mutability): Make an enum class.
(enum class): Add Async and Const enum class to avoid booleans.
(enum Unsafety): Change to an enum class.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
When making more desugaring for the HIR we can need to add new Let bindings
which will require namesolution information but also rib information for
which block the let binding is associated which was very unnessecary. This
patch simply updates the BLOCK_CONTEXT of the current scope as we are
compiling and encounter HIR::LetStmts instead of trying to do it all
upfront which lots of wierd checks
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc (HIRCompileBase::compile_locals_for_block): removed
* backend/rust-compile-base.h: update header
* backend/rust-compile-block.cc (CompileBlock::visit): remove old logic
* backend/rust-compile-expr.cc (CompileExpr::generate_closure_function): likewise
* backend/rust-compile-stmt.cc (CompileStmt::visit): likewise
* backend/rust-compile-var-decl.h: ensure we setup tuple bindings correctly
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Add a new test to check for unsafe modules during AST validation pass.
gcc/testsuite/ChangeLog:
* rust/compile/unsafe_module.rs: New test.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
An error should be emitted on unsafe modules during the AST validation
pass as the syntax allows those even though they're not alowed later down
the line.
gcc/rust/ChangeLog:
* ast/rust-item.h: Add safety getter to modules.
* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Check
a module's safety and emit an error when meeting an unsafe module.
* checks/errors/rust-ast-validation.h: Add function prototype.
* parse/rust-parse-impl.h (Parser::parse_module): Move the module locus
to the first token instead of the mod keyword.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Unsafe module are rejected at a later stage but could not be parsed
properly. This commit changes the parser in order to allow unsafe module
in the AST.
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::parse_vis_item): Dispatch to parse
module when meeting an unsafe module.
(Parser::parse_module): Set unsafe status when the parser encounter an
unsafe keyword.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
The rust syntax allows unsafe module even if those are rejected at a
later stage.
gcc/rust/ChangeLog:
* ast/rust-item.h: Add safety status to Modules in the AST.
* parse/rust-parse-impl.h (Parser::parse_module): Adapt constructors.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/ChangeLog:
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Store mappings
after having resolved them.
* resolve/rust-late-name-resolver-2.0.h: Add `TypePath` visitor.