..., which is not initialized by default, but has to be set up by every front
end individually. 'MAIN_NAME_P' enables certain code optimizations, but is
especially also relevant for back ends that emit additional program entry setup
code for 'main'.
gcc/rust/
* backend/rust-compile-base.cc (HIRCompileBase::compile_function):
For 'main', initialize 'main_identifier_node'.
..., which is not initialized by default, but has to be set up by every front
end individually. 'MAIN_NAME_P' enables certain code optimizations, but is
especially also relevant for back ends that emit additional program entry setup
code for 'main'.
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).
In #2112 'Merge upstream, last commit before "Rust front-end update 2023-04-05"'
we merged commit 939fb3fffebf7be2638ddef25dbe1c15f2aaa9d6 from upstream GCC
master branch.
- f66b135d0b5 gccrs: fatal_error_flag: Fix typo in error message
- 776ff053800 gccrs: unsafe: check use of `target_feature` attribute
- d9e05700ac3 gccrs: Check for mutable references in const functions
- ef792b9658d gccrs: rust: add bound parsing in parse_generic_arg.
- e1394230b3f gccrs: Implement declarative macro 2.0 parser
- 3f1dbb2cf8f gccrs: Add name resolution to generic argument associated item bindings
- 7eab9d18d97 gccrs: Support associated type bound arguments
- 364f4cd3072 gccrs: Reuse TypeCheckPattern on LetStmt's
- 8e2abbef4cc gccrs: Add get_locus function for abstract class MetaItemInner.
- 86f8e47f9df gccrs: diagnostics: Add underline for tokens in diagnostics.
- 881ed7fb273 gccrs: Change how CompileVarDecl outputs Bvariable's
- 60affc3a1c5 gccrs: testsuite: Handle Windows carriage returns properly
- 910e7e0a958 gccrs: Support GroupedPattern during name resolution
- cb42610bfba gccrs: Do not crash on empty macros expand. Fixes #1712
- 93fcadd208a gccrs: Add HIR lowering for GroupedPattern
- d5a303cc664 gccrs: Add get_item method for HIR::GroupedPattern
- 83e5f634d74 gccrs: Add type resolution for grouped patterns
- 221650c280c gccrs: Added missing GroupedPattern visitors for code generation
- 9b19e93873c gccrs: Rename file rust-ast-full-test.cc to rust-ast.cc
- d6cb04bf5c0 gccrs: moved operator.h to util/rust-operators.h
- 760f529d813 gccrs: fixed compiler error message on wildcard pattern within expression
- 8e7e682a9f8 gccrs: fixed indentations in AST pretty expanded dump of trait
- 51b607c2227 gccrs: macro: Allow builtin `MacroInvocation`s within the AST
- ad9d75f5080 gccrs: Create and use CompilePatternLet visitor for compiling let statments
- 9cbd270632b gccrs: parser: Allow parsing multiple reference types
- 2dab57c3f2f gccrs: Move rust-buffered-queue.h to util folder #1766
- fd7358af3cf gccrs: Improve GroupedPattern lowering
- 95efd84db13 gccrs: Remove HIR::GroupedPattern
- 0e4bf6e7fc4 gccrs: Optimize HIR::ReferencePattern
- 86f53e5853d gccrs: Implement lowering ReferencePattern from AST to HIR
- 2af66ebd1d8 gccrs: parser: Improve parsing of complex generic arguments
- 7f5144d1c96 gccrs: parser: Fix parsing of closure param list
- 476faea3563 gccrs: Add support for feature check.
- 4141941ae7b gccrs: Removed comment copy-pasted from gcc/tree.def
- 8d1d08cdbcf gccrs: Add another test case for passing associated type-bounds
- c1b1bbbc645 gccrs: Move TypePredicateItem impl out of the header
- 688fccb5227 gccrs: Refactor TyVar and TypeBoundPredicates
- e42118c327f gccrs: Refactor SubstitutionRef base class into its own CC file
- 6abbfb5280f gccrs: Refactor all substitution mapper code implementation into its own CC file
- d73082efece gccrs: Refactor BaseType, InferType and ErrorType impl into cc file
- 26af4b0604d gccrs: Refactor PathProbe into cc file
- 547e511fbdd gccrs: Refactor PathProbeType code into CC file
- 615ee14c08e gccrs: Refactor all code out of the rust-tyty.h header
- 6c4fa22d384 gccrs: Rename rust-tyctx.cc to rust-typecheck-context.cc
- 1019fd6337b gccrs: Rename header rust-hir-trait-ref.h to rust-hir-trait-reference.h
- dcb2e571ac6 gccrs: Refactor handle_substitutions to take a reference
- 2f228578d56 gccrs: Clear the substitution callbacks when copying ArgumentMappings
- 31f1e59962b gccrs: Add missing param subst callback
- e42c28bcfdd gccrs: Remove monomorphization hack to setup possible associated types
- 68d671ac725 gccrs: Refactor the type unification code
- 2a2e6712ba0 gccrs: Fix nullptr dereference
- f62dcf2c65a gccrs: Add missing Sized, Copy and Clone lang item mappings
- 9754a60623c gccrs: Fix higher ranked trait bounds computation of self
- 11150b7c05c gccrs: Remove bad error message on checking function arguments
- eb4a61ac1ba gccrs: Add general TypeBounds checks
- fa7b3a2f07f gccrs: Add support for TuplePattern in let statements
- 1c57244dbc0 gccrs: rust-item: include rust-expr.h
- f548ddee96a gccrs: parser: Expose parse_macro_invocation as public API
- 2d30e0b882f gccrs: expansion: Add `get_token_slice` to `MacroInvocLexer` class
- 3821669164d gccrs: macros: Perform macro expansion in a fixed-point fashion.
- fbd5244df6b gccrs: expander: Add documentation for `expand_eager_invocations`
- 5fd04425c09 gccrs: typecheck: Refactor rust-hir-trait-reference.h
- f64799a567e gccrs: cli: Update safety warning message
- b4229fb086b gccrs: Update copyright years.
- fd5eb9f2b01 gccrs: Add feature gate for "rust-intrinsic".
- a9d72ef2d03 gccrs: Add variadic argument type checking
- 8a9886a85ea gccrs: Add test
- e81f5be60d1 gccrs: Simplify WildcardPattern let statement handling
- b56d093e952 gccrs: lex: Prevent directories in RAIIFile
- 7b00a80c9df gccrs: testsuite: Add empty string macro test
- 20529dff93f gccrs: Add support for parsing empty tuple patterns.
- fd406fc7046 gccrs: Implemented UTF-8 checking for include_str!()
- c627781b8f6 gccrs: Extract query_type from TypeCheckBase to be a simple extern
- e90d06c5447 gccrs: Add new virtual function HIR::ImplItem::get_impl_item_name
- 4b25fc15b92 gccrs: Support for Sized builtin marker trait
- a11b44b6a57 gccrs: Fix regression in testcase
- ecdce2bf17a gccrs: Add trailing newline
- 8b0ed2387a5 gccrs: builtins: Return empty list of tokens instead of nullptr
- 6774d569c61 gccrs: Fix formatting
- 8628486f487 gccrs: Add AST::AltPattern class
- 0ec543c7c61 gccrs: Fix up DejaGnu directives in 'rust/compile/issue-1830_{bytes,str}.rs' test cases [#1838]
- 1a58da7c2d8 gccrs: rename rust-hir-full-tests.cc
- 3b342e11627 gccrs: add test case to show our query-type system is working
- fa7675df86a gccrs: ast: Refactor TraitItem to keep Location info
- 2785d591116 gccrs: diagnostic: Refactor Error class
- f2d6ab839ce gccrs: Added AST Node AST::InlineAsm
- 990ae497b86 gccrs: Address unsafe with/without block handling ambiguity
- d2a499a9881 gccrs: Fix issue with parsing unsafe block expression statements
This merge is done with 'git merge --strategy=ours', so effectively doesn't
merge any changes. Rationale: those commits are taken from GCC/Rust master
branch, and any remaining changes we'd either like to persist, or assess
individually, later.
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.
Addresses #1567
Created a AST node InlineAsm similar to the one found in rustc.
As there is no Symbol struct/class in gccrs I have made every instance
of Symbol a string.
gcc/rust/ChangeLog:
* ast/rust-ast-full-decls.h (class InlineAsm):Added class declaration.
* ast/rust-expr.h (class InlineAsm):Added class definition.
Signed-off-by: M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
Arthur Cohen [Wed, 15 Feb 2023 15:56:07 +0000 (16:56 +0100)]
gccrs: diagnostic: Refactor Error class
The class now allows for more variants including a `Hint` one which
then gets emitted by calling `rust_inform`. This allows us to display
hints/tips/notes in backtracking contexts such as the parser.
gcc/rust/ChangeLog:
* rust-diagnostics.h (struct Error): Add new Kind enum and various new
static constructors to allow for hints as well.
* rust-diagnostics.cc (Error::Error): Use new `kind` field properly.
* checks/errors/privacy/rust-visibility-resolver.cc
(VisibilityResolver::resolve_module_path): Use new Error API.
* expand/rust-macro-builtins.cc (MacroBuiltin::include_handler): Likewise.
* expand/rust-macro-expand.cc (parse_many): Likewise.
(transcribe_type): Likewise.
* parse/rust-parse-impl.h (Parser::parse_crate): Likewise.
* rust-session-manager.cc (Session::handle_crate_name): Likewise.
* ast/rust-ast.cc (Module::load_items): Likewise.
Arthur Cohen [Wed, 15 Feb 2023 16:10:54 +0000 (17:10 +0100)]
gccrs: ast: Refactor TraitItem to keep Location info
gcc/rust/ChangeLog:
* ast/rust-ast.h: Keep location in TraitItem base class
* ast/rust-item.h (class TraitItemFunc): Use base class location instead.
(class TraitItemMethod): Likewise.
(class TraitItemConst): Likewise.
(class TraitItemType): Likewise.
* ast/rust-macro.h: Likewise.
Thomas Schwinge [Tue, 14 Feb 2023 23:28:35 +0000 (00:28 +0100)]
gccrs: Fix up DejaGnu directives in 'rust/compile/issue-1830_{bytes,str}.rs' test cases [#1838]
'dg-excess-errors' "indicates that the test is expected to fail due to compiler
messages that are not handled by 'dg-error', [etc.]". It's argument doesn't
specify the message to match but just just a comment. Here, however, we'd like
to match specific compiler messages, so we should use 'dg-error', etc. Use the
magic line number zero as the compiler error diagnostic doesn't tell which line
the error occurred on (separate issue to resolve). This gives us:
-XFAIL: rust/compile/issue-1830_str.rs (test for excess errors)
+PASS: rust/compile/issue-1830_bytes.rs at line 8 (test for errors, line )
+PASS: rust/compile/issue-1830_bytes.rs (test for excess errors)
-XFAIL: rust/compile/issue-1830_bytes.rs (test for excess errors)
+PASS: rust/compile/issue-1830_str.rs at line 8 (test for errors, line )
+PASS: rust/compile/issue-1830_str.rs (test for excess errors)
Philip Herron [Mon, 13 Feb 2023 17:51:19 +0000 (17:51 +0000)]
gccrs: Support for Sized builtin marker trait
When implementing general bounds checking as part of unify calls, we did
not check associated types on bounds which lead to alot of missed error
checking. This now recursively checks the bounds and the associated types
with a decent error message. This also required us to implement the Sized
marker trait to keep existing test-cases happy.
Fixes #1725
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:
* typecheck/rust-hir-trait-reference.cc (TraitReference::clear_associated_types): make const
(TraitReference::clear_associated_type_projections): new interface
* typecheck/rust-hir-trait-reference.h:
* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait): refactor
(TraitItemReference::associated_type_reset): reset projections
* typecheck/rust-hir-type-bounds.h:
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): fix bounds
* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::TypeBoundsProbe): refactor into cc file
(TypeBoundsProbe::Probe): refactor
(TypeBoundsProbe::is_bound_satisfied_for_type): likewise
(TypeBoundsProbe::assemble_sized_builtin): add builtin for Sized
(TypeCheckBase::get_predicate_from_bound): refactor
(TypeBoundPredicate::lookup_associated_type): refactor
* typecheck/rust-tyty-subst.cc (SubstitutionRef::lookup_associated_impl)
(SubstitutionRef::prepare_higher_ranked_bounds): new interface to clear hanging bounds
(SubstitutionRef::monomorphize): refactor
* typecheck/rust-tyty-subst.h:
* typecheck/rust-tyty.cc (BaseType::get_locus): helper
(BaseType::satisfies_bound): ensure bounds are satisfied and assoicated types
(ParamType::ParamType): new field in constructor
(ParamType::clone): update clone
(ParamType::set_implicit_self_trait): new interface
(ParamType::is_implicit_self_trait): likewise
* typecheck/rust-tyty.h: cleanup
* util/rust-hir-map.cc (Mappings::Mappings): builtin marker
(Mappings::~Mappings): delete marker
(Mappings::lookup_builtin_marker): lookup
* util/rust-hir-map.h: update header
gcc/testsuite/ChangeLog:
* rust/compile/issue-1725-1.rs: New test.
* rust/compile/issue-1725-2.rs: New test.
RAIIFile constructor was accepting directory filename. This lead to
unattended directory opening in some part of the code (load_file_bytes)
wich resulted in ice. Since RAIIFile are used for the lexer, removing
the ability to open directories with RAIIFile fixes those issues and
prevent future mistakes.
Owen Avery [Fri, 3 Feb 2023 15:19:32 +0000 (10:19 -0500)]
gccrs: Simplify WildcardPattern let statement handling
gcc/rust/ChangeLog:
* backend/rust-compile-pattern.cc
(CompilePatternLet::visit): Simplify WildcardPattern compilation for let statements.
* backend/rust-compile-var-decl.h:
(CompileVarDecl::visit): Remove variable declaration for WildcardPattern.
* resolve/rust-ast-resolve-pattern.h:
(PatternDeclaration::visit): Remove name resolution for WildcardPattern.
Arthur Cohen [Wed, 18 Jan 2023 11:23:03 +0000 (12:23 +0100)]
gccrs: macros: Perform macro expansion in a fixed-point fashion.
This commit changes our macro expansion system from an eager and recursive
macro expansion to a fixed-point like system. Instead of, when seeing
a macro invocation, expanding it and all of the macros within it, we
now perform multiple passes of expansion on the entire crate.
This, however, leads to a problem. Rust macros are expanded lazily, but
Rust builtin macros should be expanded eagerly. Due to this, we must
work around the lazy expansion in builtin macros and perform eager
expansion for each pass of the fixed-point, before finally expanding
the builtin when there are no longer any inner macro invocations.
To perform proper macro scoping, the ENR now keeps track of the current
scope (`current_scope` member) and resolves macros accordingly.
This is done through the use of the `scoped` method, which creates a new
scope, runs a specified lambda and then exits the scope. This prevents
pushing/popping errors that we've seen happen already in similar
contexts.
We might think about generalizing it to other classes, providing a
`Scoped<EntryFn, ExitFn>` class or similar
gcc/rust/ChangeLog:
* ast/rust-macro.cc: New file.
* Make-lang.in: Add `rust-macro.o` object
* ast/rust-ast-fragment.cc (Fragment::Fragment): Change API around
the construction of AST fragments.
(Fragment::operator=): Correct `Fragment::operator=` to take into
account the fragment tokens.
(Fragment::create_error): Use new constructor.
(Fragment::complete): Remove in favor of new constructor.
(Fragment::unexpanded): Remove as that Fragment type is no longer used
or possible.
(Fragment::get_tokens): Add helper to access a fragment's tokens.
* ast/rust-ast-fragment.h (enum class): Remove `FragmentKind::Unused`
* ast/rust-ast.cc (MacroInvocation::as_string): Display
builtin macro invocations properly.
* ast/rust-ast.h: Fix `DelimTokenTree` class copy constructors and
handling of its token vector.
* ast/rust-macro.h (class MacroMatcher): Format.
(class MetaItemSeq): Likewise.
(builtin_macro_from_string): Get a `BuiltinMacroKind` from a given
string, i.e the name of the macro (`assert!`, `cfg!` and so on).
* expand/rust-attribute-visitor.cc (AttrVisitor::visit): Do not expand
macros recursively anymore.
(AttrVisitor::maybe_expand_expr): Likewise.
(AttrVisitor::maybe_expand_type): Likewise.
* expand/rust-attribute-visitor.h: Likewise, and remove
`expand_macro_fragment_recursively` function.
* expand/rust-macro-builtins.cc (make_token): Add shorthand for
returning `std::unique_ptr<AST::Token>`s.
(make_macro_invocation): Add shorthand for returning fragments
containing builtin macro invocations.
(try_expand_macro_expression): Do not expand macros recursively.
(try_expand_single_string_literal): Likewise.
(try_expand_many_expr): Likewise.
(parse_single_string_literal): Error out more appropriately.
(MacroBuiltin::compile_error_handler): Add explanation for eager
invocation
(MacroBuiltin::file_handler): Return the proper tokens associated with
macro invocation, and builtin macros in the case of necessary eager
expansion.
(MacroBuiltin::column_handler): Likewise.
(MacroBuiltin::include_bytes_handler): Likewise.
(MacroBuiltin::include_str_handler): Likewise.
(MacroBuiltin::concat_handler): Likewise.
(MacroBuiltin::env_handler): Likewise.
(MacroBuiltin::cfg_handler): Likewise.
(MacroBuiltin::include_handler): Likewise.
(MacroBuiltin::line_handler): Likewise.
* expand/rust-macro-expand.cc (MacroExpander::expand_eager_invocations):
Add function to expand eager invocations *once* in the fixed point
pipeline.
(MacroExpander::expand_invoc): Call into `expand_eager_invocations` for
builtin macro invocations.
(MacroExpander::expand_crate): Use new `AttrVisitor` API.
(parse_many): Return tokens in `AST::Fragment`.
(transcribe_expression): Likewise.
(transcribe_type): Likewise.
* expand/rust-macro-expand.h (struct MacroExpander): Add `has_changed`
flag for fixed point checking.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::EarlyNameResolver):
Keep track of the current macro scope.
(EarlyNameResolver::go): Use `scoped` API.
(EarlyNameResolver::visit): Likewise.
* resolve/rust-early-name-resolver.h: Add `scoped` API.
* rust-session-manager.cc (Session::expansion): Perform macro expansion
in a fixed-point fashion.
gcc/testsuite/ChangeLog:
* rust/compile/macro17.rs: Fix testsuite for new recursion errors.
* rust/compile/macro44.rs: Fix invalid testcase assertions.
* rust/compile/builtin_macro_recurse.rs: Fix invalid test.
* rust/compile/builtin_macro_recurse2.rs: New test.
* rust/compile/macro46.rs: New test.
Philip Herron [Tue, 31 Jan 2023 18:52:33 +0000 (18:52 +0000)]
gccrs: Fix higher ranked trait bounds computation of self
This updates the higher ranked trait bounds computation to handle ambigious
cases. When we have a slice for example:
let slice = &a[1..3];
This works by reusing the Index operator overload from libcore, so when the
index range of 1..3 is computed, the type system needs to compute what the
types of index are; this works by integer inference variables
Range<<integer>> that need to be unified with the impl Index for
Range<Usize> which computes the real type of usize for the index. This is
fine but what happens when we have the Copy and Clone traits bounds which
have implementations for all the primitive types i8, i16, i32, i64...
which is valid for any integer inference variable so the code prior to this
patch would have grabbed the first impl it would have found and used it
which is incorrect. When we have integer or float inference variables we
need to look for their respective defaults or emit an ambigious type bound
error.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:
Philip Herron [Tue, 31 Jan 2023 14:27:49 +0000 (14:27 +0000)]
gccrs: Fix nullptr dereference
When we check if this is concrete the guard checks to ensure the argument
is non null but the check here is wrongly returning early when the check
is non null meaning when it is null and therefore not concrete it will
end up doing a null dereference.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:
Philip Herron [Mon, 30 Jan 2023 18:19:07 +0000 (18:19 +0000)]
gccrs: Refactor the type unification code
This refactors the unification systems to be a consistent interface using
switch statements and simple functions instead of the old clunky visitor
system. This is more maintainable as it is harder to miss cases when we
can take advantages of switch statements.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:
Philip Herron [Fri, 27 Jan 2023 15:38:58 +0000 (15:38 +0000)]
gccrs: Remove monomorphization hack to setup possible associated types
During CallExpr argument type checking we may be calling a default
implementation of a trait function this will require any possible
associated types to be resolved and setup. This monomoprhization call does
this but it will premtivly do extra unification of types which will throw
off type checking later on. This fix is required for my work into type
bounds checking.
Fixes #1773
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:
Philip Herron [Fri, 27 Jan 2023 18:31:11 +0000 (18:31 +0000)]
gccrs: Clear the substitution callbacks when copying ArgumentMappings
When we set the callback on substitutions this is not safe to be copied
around since we store the used argument mappings and can reuse them in
different contexts. This clears the callback on copy's to make it safer.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:
Philip Herron [Fri, 27 Jan 2023 18:28:06 +0000 (18:28 +0000)]
gccrs: Refactor handle_substitutions to take a reference
This patch changes the recusive substitution code to take a reference
instead of a copy. This is important as the callback field is going to be
made non-copyable in a future patch and this pipeline is for recursive
substitutions so its ok to reuse the same mappings here.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:
The parser now recursively tries to parse a reference type after seeing
a `&` or `&&` token.
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::parse_type): Handle double ampersan
properly
(Parser::parse_reference_type): Call into `parse_reference_type_inner`
and wrap double reference types in another `AST::ReferenceType` node
(Parser::parse_reference_type_inner): Add parsing implementation
which does not care about the leading token (& or &&)
(Parser::parse_type_no_bounds): Handle double ampersand properly
* parse/rust-parse.h: Declare `parse_reference_type_inner`