* libproc_macro/ident.cc (Ident__new): Use named
constructor.
(Ident__new_raw): Use named constructor.
(Ident__clone): Use clone member function.
(Ident::clone): Make clone const.
(Ident::make_ident): Add named construcot.
* libproc_macro/ident.h (struct Ident): Add named
constructor prototypes.
Fixes #2021, #2022
Deleted Parser::debug_dump_ast_output, removed any functions that called
it i.e Session::dump_ast and Session::dump_ast_expanded, and any
associated items.
Made it so that when you use the dump option "expanded" it dumps the
pretty ast only.
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::debug_dump_ast_output): Removed this funtion.
* rust-session-manager.cc (Session::enable_dump): Removed else if (arg == "parse").
(Session::compile_crate): Removed calls of dump_ast and dump_ast_expanded.
(Session::dump_ast): Removed this function.
(Session::dump_ast_expanded): Removed this function.
* rust-session-manager.h (struct CompileOptions): Removed the PARSER_AST_DUMP option.
Signed-off-by: M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
Arthur Cohen [Wed, 19 Apr 2023 15:40:15 +0000 (17:40 +0200)]
gccrs: parser: Parse reference patterns correctly
Reference patterns cannot contain AltPatterns per the Rust reference,
so we should not call into `parse_pattern` to parse the referenced pattern,
but rather the more restrictive `parse_pattern_no_alt`.
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::parse_reference_pattern): Do not
call into `parse_pattern` anymore.
gccrs: libproc_macro: Drop function shall take a mutable
The rust API requires a mut reference, hence having a mutable pointer
seems to match better. Furthermore the implementation is now modifying
the struct in order to set the size to 0 instead of simply freeing the
data, this will allow us to easily identify mistaken manual call to this
function.
libgrust/ChangeLog:
* libproc_macro/rust/bridge/literal.rs: Make the
pointer mutable.
Add literal type related ffi functions implementation.
libgrust/ChangeLog:
* libproc_macro/literal.h (Literal__drop): Implement
drop.
(Literal__string): Constructor from a string function prototype.
(Literal__byte_string): Constructor from a byte string function
prototype.
(Literal__from_string): Add function body, no implementation
yet.
* libproc_macro/literal.cc: New file.
When we encounter a "break rust" statement, emit a funny error message
and intentionally cause an ICE. This matches the corresponding Easter
egg in rustc. As a GNU extension, "break gcc" is also supported.
The conditions for this to happen are:
* The break expression must be literally "rust" or "gcc". For instance,
"break (rust)" will not trigger the Easter egg.
* The name ("rust" or "gcc") must not be in scope; if it is, no error
is emitted, and the compilation proceeds as usual. In other words,
this only affects how GCC diagnoses programs that would fail to
compile anyway.
Note that this is different from the conditions under which rustc emits
its ICE. For rustc, it matters whether or not the "break" is inside a
loop, and for us it matters whether or not the name resolves. The end
result should be the same anyway: valid programs continue to compile,
and typing in
gcc/testsuite/ChangeLog:
* lib/prune.exp (prune_ices):
Also prune "You have broken GCC Rust. This is a feature."
* rust/compile/break-rust1.rs: New test
* rust/compile/break-rust2.rs: New test
* rust/compile/break-rust3.rs: New test
We're going to introduce AST::Kind::IDENTIFIER next, and with the
default C-style enum member scoping, this would cause name clashes.
Instead, convert AST::Kind into an enum class, so that its members
are properly namespaced.
gcc/rust/ChangeLog:
* ast/rust-ast.h (Kind): Convert into a C++ enum class
* expand/rust-macro-builtins.cc: Adapt to the change
Marc Poulhiès [Thu, 6 Apr 2023 17:20:55 +0000 (19:20 +0200)]
gccrs: Introduce AST::Visitable class for AST
AST::Visitable is an abstract class with a unique accept_vis() method.
Make all abstract AST node class inherit from this class.
Allows for easy definition of operations on nodes that must accept a
visitor.
The static Dump::dump() is an example of such use: the static method
accepts any AST node, creates a Dump visitor and have it visit the AST
starting at the node.
This change also inserts a debug(Visitable&) function in the global
namespace to make it easy to call from the debugger (similar to
debug_tree or debug(rtx*) functions).
Add a boolean to tell inner and outer attributes ast nodes appart. This
meant refactoring a bit their parsing function.
gcc/rust/ChangeLog:
* ast/rust-ast.h: Add boolean for differenciation.
* parse/rust-parse-impl.h (Parser::parse_doc_comment): Change
function interface to make code cleaner. It should return a body
instead of the whole attribute.
(Parser::parse_inner_attribute): Specify the inner status of the
node.
(Parser::parse_attribute_body): Change function interface to
make the code cleaner much like parse_doc_comment.
(Parser::parse_outer_attribute): Specify outer status of the
node.
* parse/rust-parse.h: Update functions prototypes.
gccrs: expand: Add stub function for attribute expansion
Add a stub function and utility functions that should be called on some
nodes to expand attribute procedural macros.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc (ExpandVisitor::expand_outer_attribute):
Stub for a single attribute expansion.
(ExpandVisitor::visit_outer_attrs): Visit the attributes to
expand on a given item.
* expand/rust-expand-visitor.h: Add function prototypes.
gccrs: ast: Add outer attribute getter to Expr class
We need to retrieve outer attributes from some Expressions depending on
their context. This means this should be retrieved from their parent
node. But expr did not have a getter for outer attributes since some
expr can't have any outer attribute.
gcc/rust/ChangeLog:
* ast/rust-ast.h: Add getter to Expr class.
* ast/rust-expr.h: Add override attribute to existing getters.
Also implement it for RangeExpr, attempting to retrieve outer
attributes on those types will crash the compiler.
* ast/rust-macro.h: Add override attribute to existing getters.
* ast/rust-path.h: Likewise.
Add function stubs for inner attribute proc macros expansion.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Add call
to inner attribute stub.
(ExpandVisitor::expand_inner_attribute): Expand one single
attribute.
(ExpandVisitor::visit_inner_using_attrs): Expand a given item
using a vector of attributes.
(ExpandVisitor::visit_inner_attrs): Visit inner attributes of a
given item.
* expand/rust-expand-visitor.h: Add function prototypes.
* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Add call
to derive expander.
(ExpandVisitor::expand_derive): Expand a single derive.
(ExpandVisitor::visit_attrs_with_derive): Visit an item with
derive attributes.
(ExpandVisitor::is_derive): Identify a derive attribute.
* expand/rust-expand-visitor.h: Add function prototypes.
Attributes were not converted to a correct tokenstream, furthermore meta
items containers and token trees attribute arguments were not even
implemented. This commit fix the literal attribute type and implement
both unimplemented types.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (TokenStream::visit): Fix existing
and implement remaining attribute visitors.
Even though the comma may be optional with blocks of expressions, it is
mandatory for a single expression alone. By changing this behavior we
ensure a working behavior with any kind of expressions.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (TokenStream::visit): Make comma
mandatory.
The visitor created some additional semicolon between statements in
extern blocks, this lead to empty statement.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (TokenStream::visit): Remove
additional semicolon output.
* ast/rust-ast-tokenstream.h: Change block visitor prototype
with a default value for trailing tokens.
Philip Herron [Sun, 2 Apr 2023 20:59:50 +0000 (21:59 +0100)]
gccrs: Fix ICE using constructors for intilizers in statics
We are getting constant expressions for the initilizers for static items
this hits an assertion in the GCC middle-end which is looking for a
constructor so we need to unwrap the constant expression using DECL_INITIAL
as the initilizer to the global static.
Fixes #2080
gcc/rust/ChangeLog:
* backend/rust-compile-item.cc (CompileItem::visit): unwrap the constant expression
gcc/testsuite/ChangeLog:
* rust/execute/torture/issue-2080.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
In rust cast to char is allowed only from u8 type.
This patch handles fix the case when the type is infered from an integer value,
allowing only the u8 case'
Fixes #2027
gcc/rust/ChangeLog:
* typecheck/rust-casts.cc (TypeCastRules::cast_rules):
case INTEGRAL handles TypeKind::CHAR
Philip Herron [Wed, 29 Mar 2023 12:35:53 +0000 (13:35 +0100)]
gccrs: Fix handling of generic arguments for lifetimes only
We might have generics that are only for lifetimes this is a bad error
check. We can simply rely on the function to marshall the HIR generics
arguements for the type system to do the error handling for us which will
be more acurate anyway.
Fixes #2043 #2039
gcc/rust/ChangeLog:
* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit):
add check for reference marker and type's in self params
(ResolveItem::visit): likewise
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_root_path):
remove bad generics check
(TypeCheckExpr::resolve_segments): likewise
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): likewise
gcc/testsuite/ChangeLog:
* rust/compile/const_generics_5.rs: update test case
* rust/compile/issue-2043.rs: New test.
* rust/compile/issue-2039.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Wed, 29 Mar 2023 13:24:12 +0000 (14:24 +0100)]
gccrs: add missing name resolution to self params with specified types
In rust it is possible to write method where you explicitly specify the
type of a self param. It does not however allow for you to use reference
destructuring in this senario.
gcc/rust/ChangeLog:
* resolve/rust-ast-resolve-item.cc
(ResolveTraitItems::visit): add name resolution self param
(ResolveItem::visit): likewise
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Wed, 29 Mar 2023 14:00:39 +0000 (15:00 +0100)]
gccrs: make distinction between error and empty for substution args
When handling generics of only lifetimes we have an empty
SubstitutionArgumentMappings which was being detected as an error but this
is not the case as we don't currently handle const generics or generic
lifetimes so this makes the distinction between an error occurred or its
simply empty because it was all only lifetime arguments and we don't care
about it.
Addresses #2043 #2039
gcc/rust/ChangeLog:
* typecheck/rust-tyty-bounds.cc (TypeBoundPredicate::TypeBoundPredicate): this is an empty
(TypeBoundPredicate::operator=): likewise
* typecheck/rust-tyty-subst.cc (SubstitutionArgumentMappings::empty): new interface
(SubstitutionArgumentMappings::is_error): this marks it as an error
* typecheck/rust-tyty-subst.h: update prototypes
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Add an option to the command line interface to dump the ast back to a
tokenstream.
gcc/rust/ChangeLog:
* rust-session-manager.cc (Session::enable_dump): Enable new
dump.
(Session::compile_crate): CLI argument parsing.
(Session::dump_tokenstream): Dump the tokenstream as a string in
the specified file.
* rust-session-manager.h (struct CompileOptions): Add
tokenstream dump option.
Add an option to output trailing commas depending on the configuration
of the TokenStream.
gcc/rust/ChangeLog:
* ast/rust-ast-tokenstream.cc (TokenStream::trailing_comma):
Output a trailing comma to the token stream according to the
configuration.
* ast/rust-ast-tokenstream.h: Add function prototype.
Add several functions to convert an ast back to a token stream. This may
be used later either to unify the ast dumping and modifications on ast
nodes in procedural macros.
gcc/rust/ChangeLog:
* Make-lang.in: Add rust-as-tokenstream to compile list.
* ast/rust-item.h: Add missing getter for location.
* ast/rust-ast-tokenstream.cc: Add ast visitor implementation.
* ast/rust-ast-tokenstream.h: New file.
* ast/rust-pattern.h: Add getter.
Philip Herron [Wed, 29 Mar 2023 15:28:11 +0000 (16:28 +0100)]
gccrs: handle bare function types with no specified return type
When we have a function with no return type this defaults to (), but we
need to be sure we generate a new implicit HirId for it otherwise it will
end up in a recursive reference chain.
Fixes #2042
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit):
Add implicit unit type as the return type when not specified
gcc/testsuite/ChangeLog:
* rust/compile/issue-2042.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Wed, 29 Mar 2023 14:57:56 +0000 (15:57 +0100)]
gccrs: fix ICE with bad match arm type
We expect to get some kind of ADT or Tuple type when computing this kind of
match arms this adds a new diagnostic to check for this case instead of
an assertion.
Fixes #2029
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): return early on bad type
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::TypeCheckPattern):
remove assertion in favor of check
(TypeCheckPattern::visit): likewise
gcc/testsuite/ChangeLog:
* rust/compile/issue-2029.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
* libproc_macro/rust/bridge.rs: New file.
* libproc_macro/rust/bridge/group.rs: New file.
* libproc_macro/rust/bridge/ident.rs: New file.
* libproc_macro/rust/bridge/literal.rs: New file.
* libproc_macro/rust/bridge/punct.rs: New file.
* libproc_macro/rust/bridge/span.rs: New file.
* libproc_macro/rust/bridge/token_stream.rs: New file.
* libproc_macro/rust/group.rs: New file.
* libproc_macro/rust/ident.rs: New file.
* libproc_macro/rust/lib.rs: New file.
* libproc_macro/rust/literal.rs: New file.
* libproc_macro/rust/punct.rs: New file.
* libproc_macro/rust/span.rs: New file.
* libproc_macro/rust/token_stream.rs: New file.
Arthur Cohen [Tue, 28 Mar 2023 12:22:21 +0000 (14:22 +0200)]
gccrs: privacy: Check for pub(crate) when resolving visibility path.
This causes the function to return true and the checks to pass, but it
requires more thinking - how do we deal with pub(crate) in the current system?
Should we simply treat it as a pub item in the current crate, but export it as
a private item in the metadata?
gcc/rust/ChangeLog:
* ast/rust-item.h: Fix `Visibility::has_path()` implementation.
* checks/errors/privacy/rust-visibility-resolver.cc
(VisibilityResolver::resolve_module_path): Check if we are dealing with pub(crate) properly.
Arthur Cohen [Tue, 28 Mar 2023 11:55:19 +0000 (13:55 +0200)]
gccrs: resolver: Allow SimplePath to resolve to their root segment
SimplePath are allowed to resolve to their root segment even if it is `crate`
or `super` when checking visibilities. We now have to make sure that this
is allowed everywhere a SimplePath is kept.
gcc/rust/ChangeLog:
* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): Set
resolved_node_id when visiting root segment of SimplePath.
Arthur Cohen [Tue, 28 Mar 2023 09:34:27 +0000 (11:34 +0200)]
gccrs: ast: Fix location for pub(crate)
When creating a SimplePath from a string, we end up creating a path
which contains only one segment but no location. Take the location of
the one segment instead and use it as the simple path's location.
gcc/rust/ChangeLog:
* ast/rust-ast.h: Call SimplePath constructor differently in
SimplePath::from_str