Philip Herron [Sun, 23 Apr 2023 22:12:45 +0000 (23:12 +0100)]
gccrs: Add missing ABI checking on function types
Addresses #2304
gcc/rust/ChangeLog:
* typecheck/rust-unify.cc (UnifyRules::emit_abi_mismatch): new error method
(UnifyRules::expect_fndef): add ABI check
* typecheck/rust-unify.h: prototype for new error method
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Sun, 23 Apr 2023 21:41:21 +0000 (22:41 +0100)]
gccrs: Add mechanism use pattern information to improve type info
When we have an untyped closure we assumed all parameters were inference
variables but we can use the pattern type to try and improve the type info
so if we have a reference pattern it must be a reference to an inference
variables '&_'. This patch adds a new visitor to figure this out for
untyped closure parameters.
Note this test case does fully type resolve into the gimple:
Though the Rustc version does fail type-resolution but we make some
assumptions during comparison expressions here that they resolve to a bool
this will change when we implement the comparison lang items.
Fixes #2142
gcc/rust/ChangeLog:
* hir/tree/rust-hir-pattern.h: add missing get_mutability
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
use new ClosureParamInfer on untyped parameters
* typecheck/rust-hir-type-check-pattern.cc (ClosureParamInfer::Resolve): interface
(ClosureParamInfer::ClosureParamInfer): constructor
(ClosureParamInfer::visit): visitors for each pattern
* typecheck/rust-hir-type-check-pattern.h (class ClosureParamInfer): new visitor
gcc/testsuite/ChangeLog:
* rust/compile/issue-2142.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Thu, 20 Apr 2023 11:44:31 +0000 (12:44 +0100)]
gccrs: Fix ICE during method resolution
We were missing a check for trait item selection to ensure they are
actually methods and remove assertion to check if the trait item is a
function this is a valid error check not an assertion.
Fixes #2139
gcc/rust/ChangeLog:
* typecheck/rust-hir-dot-operator.cc (MethodResolver::select): verify it is a method
gcc/testsuite/ChangeLog:
* rust/compile/issue-2139.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Tue, 18 Apr 2023 16:56:43 +0000 (17:56 +0100)]
gccrs: Track Self properly with TypePredicateBounds
When we handle generic trait bounds we never tracked its associated Self
type. Its important to remember a Trait Predicate is associated with a type
this means we end up missing a lot of helpful type information down the
line relating to higher ranked trait bounds and associated types
compuations. Remember traits have an implict Generic Type Parameter of Self
we use this in computing trait defintions so in that case no associated
type is specified which is to be expected but in all other cases we do
even if it is generic its still useful type information to keep track of.
There is one regression here with compile/issue-1893.rs this testcase
mostly worked out as a fluke rather than a proper fix so its no suprise
here it has regressed the other two test cases one where the number
generic arguments has changed, Rustc gets around this and has a seperate
error message for this case.
We need to solve this patch in order to solve #2019
Philip Herron [Tue, 18 Apr 2023 11:36:29 +0000 (12:36 +0100)]
gccrs: add error state to TypeCheckContextItem and missing copy ctor's
When checking current context we might be in the const or static context
which does not have a current function or impl or trait context associated
with it. So this allows us to represent the an error state for that case.
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check.h: New error state and missing copy implementations
* typecheck/rust-typecheck-context.cc (TypeCheckContextItem::TypeCheckContextItem):
missing copy ctor
(TypeCheckContextItem::operator=): missing copy implementation
(TypeCheckContextItem::get_error): new static function
(TypeCheckContextItem::is_error): new method
(TypeCheckContextItem::get_context_type): handle error state
(TypeCheckContextItem::get_defid): handle error state
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Mon, 17 Apr 2023 19:48:41 +0000 (20:48 +0100)]
gccrs: Fix memory corruption at peek_context
When working in the resolve_operator_overload it was found that we got
memory corruption as method resolution will use the query system and
therefore resolve new methods and the current function context info will
change and due to the fact the peek_context interface returns a reference
to the element which was now safe from a vector which can change and all
you need is the current function context at that moment in time.
gcc/rust/ChangeLog:
* typecheck/rust-autoderef.cc: don't take a reference
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): likewise
* typecheck/rust-hir-type-check.h: remove reference
* typecheck/rust-typecheck-context.cc (TypeCheckContext::pop_return_type): likewise
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gccrs: libproc_macro: Add remaining drop functions
Remaining structures from the rust bridge that missed a drop function
now have one.
libgrust/ChangeLog:
* libproc_macro/group.cc (Group::drop): Add drop
implementation.
* libproc_macro/group.h: Add drop prototype.
* libproc_macro/tokenstream.cc (TokenStream::drop): Add
drop implementation.
(TokenStream__drop): Change to a call to TokenStream::drop.
* libproc_macro/tokenstream.h: Add drop prototype.
* libproc_macro/tokentree.cc (TokenTree::drop): Add
drop implementation.
* libproc_macro/tokentree.h: Add drop prototype.
gccrs: libproc_macro: Add drop function to Literal struct
Add a drop function to clean internal fields of a Literal struct.
libgrust/ChangeLog:
* libproc_macro/literal.cc (Literal__drop): Replace
implementation by a call to Literal::drop.
(Literal::drop): Add drop implementation.
* libproc_macro/literal.h: Add function prototype.
Add remaining missing tokenstream structures. Most are interdependent.
libgrust/ChangeLog:
* libproc_macro/group.cc: New file.
* libproc_macro/group.h: New file.
* libproc_macro/tokenstream.cc: New file.
* libproc_macro/tokenstream.h: New file.
* libproc_macro/tokentree.cc: New file.
* libproc_macro/tokentree.h: New file.
gccrs: Fix translation mistakes 2023 in GCC/Rust [PR108890]
In https://gcc.gnu.org/PR108890 "Translation mistakes 2023"
@rillig lists several issues with GCC/Rust diagnostics and
option help texts (but also a few non-GCC/Rust).
This commit fix mistakes only related to GCC/Rust,
specifically to the file `gcc/rust/lang.opt`.
* 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>