Muhammad Mahad [Thu, 6 Jul 2023 12:08:56 +0000 (17:08 +0500)]
gccrs: [E0061] Refactored argument mismatch error function
Added Invalid number of arguments (argument mismatch)
was passed when calling a function - unexpected
number of arguments `x` expected `y` And Refactored
error into one function.
gcc/rust/ChangeLog:
* typecheck/rust-tyty-call.cc (emit_unexpected_argument_error):
Refactored invalid number of argument into one function.
(TypeCheckCallExpr::visit): called refactored function.
(TypeCheckMethodCallExpr::check): likewise.
gcc/testsuite/ChangeLog:
* rust/compile/func2.rs: updated comment to pass new test cases.
* rust/compile/tuple_struct2.rs: likewise.
* rust/compile/wrong_no_of_parameters.rs: New test.
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Arthur Cohen [Tue, 27 Jun 2023 13:56:04 +0000 (15:56 +0200)]
gccrs: nr2.0: Add complex testcases
gcc/testsuite/ChangeLog:
* rust/compile/name_resolution1.rs: New test.
* rust/compile/name_resolution2.rs: New test.
* rust/compile/name_resolution3.rs: New test.
* rust/compile/name_resolution4.rs: New test.
* rust/compile/name_resolution5.rs: New test.
Arthur Cohen [Fri, 23 Jun 2023 14:37:16 +0000 (16:37 +0200)]
gccrs: optional: Import tl's optional type
This is directly adapted from https://github.com/TartanLlama/optional.
This class is tested for C++11 and on GCC 4.8, which is our target mimimum
GCC version.
This optional class provides multiple advantages over our existing
implementation of Optional<T>:
- Easier API (no static constructors)
- Better method chaining (and_then, map...)
- Harsher semantics (no assign-through on Optional<T&>)
Over the std::optional type present in C++'s standard library, this type offers:
- Optional references without going through std::reference_wrapper
- Better method chaining
For these reasons, I am in favor of importing it directly rather than
adapt C++14's std::optional type to work with C++11 and GCC 4.8.
Philip Herron [Fri, 30 Jun 2023 21:08:10 +0000 (22:08 +0100)]
gccrs: fix ICE with inserting autoderef mappings
We were using the call-expr id for the autoderef mappings but this doesn't
work when the call expression itself is part of a coercion-site so this
changes the code to use the call-expression function path expression for
the id instead which will not be duplicated again.
Addresses #2105
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (CompileExpr::generate_possible_fn_trait_call): use fnexpr
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::resolve_fn_trait_call): likewise
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Type hint value was not propagated correctly to LiteralPattern in ast.
This was defaulted to CORETYPE_STR instead, which introduced several
bugs with systems that did require any ast collection.
gcc/rust/ChangeLog:
* ast/rust-pattern.h: Change constructor to accept new parameter
instead of defaulting on string type.
* parse/rust-parse-impl.h (Parser::parse_literal_or_range_pattern):
Propagate type hint.
(Parser::parse_pattern_no_alt): Likewise.
Muhammad Mahad [Thu, 29 Jun 2023 14:24:00 +0000 (19:24 +0500)]
gccrs: refactored field error function
Refactored field error function into one funciton
Fixes #2336
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-pattern.cc (emit_invalid_field_error): Refactored Field
Error Function
(TypeCheckPattern::visit): called emit_invalid_field_error function
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Philip Herron [Thu, 29 Jun 2023 09:13:01 +0000 (10:13 +0100)]
gccrs: fix how we handle coercions and casts of unsafe pointers
I copied swap_nonoverlapping badly from libcore which uses an unsafe
pointer case of x as *mut u8, I thought this was x as *mut T when i was
copying code form libcore this caused new infererence variables to be
introduced which actually automatically turned this swap_nonoverlapping
function to turn into a swap_nonoverlapping<u8> and lost the general
generic one in the type system context.
Rust allows for you to cast any pointer to another pointer of different
base type. The rules here are really subtle though because we need to
handle the case where we do really need to unify the types here there
are a few cases to consider but the main three are:
*u32 vs *u32 -> valid pointers match = simple coercion
*<?> vs *u8 -> inference variable unified with u8 which is valid
*T vs *u8 -> invalid coercion as the element types don't match
-> But this is a valid cast site
The code for casts try a coercion first then if that was sucsessful goes
on to perform a real coercion. Otherwise it follows the cast rules. The
bug here was that we saw the case of *T vs *u8 the try coercion used
inference variables to match T vs u8 but this will cause a cascase of
bad inference variables which we don't want when we perform the simple
coercion.
Fixes #2330
gcc/rust/ChangeLog:
* typecheck/rust-casts.cc (TypeCastRules::check): apply new argument
* typecheck/rust-coercion.cc (TypeCoercionRules::Coerce): track if this is a cast site
(TypeCoercionRules::TryCoerce): update ctor
(TypeCoercionRules::TypeCoercionRules): cleanup error handling
(TypeCoercionRules::do_coercion): likewise
(TypeCoercionRules::coerce_unsafe_ptr): only infer in try mode
(TypeCoercionRules::coerce_borrowed_pointer): cleanup
* typecheck/rust-coercion.h: update header
gcc/testsuite/ChangeLog:
* rust/compile/issue-1981.rs: Fix badly copied libcore code test
* rust/compile/issue-2330.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Thu, 29 Jun 2023 09:13:29 +0000 (10:13 +0100)]
gccrs: Track associated type generics
This fixes an issue with our qualified type-paths if we point to a generic
associated type this needs to either use the placeholder type on the trait
reference or use the impl-type but apply the generics.
gcc/rust/ChangeLog:
* typecheck/rust-hir-trait-reference.h: update prototype
* typecheck/rust-hir-trait-resolve.cc: add generic args as an out param
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): use the generics
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gccrs: collector: Store CollectItem instead of TokenPtr
Store a custom type to differentiate formatting and metadata from data.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::collect_tokens):
Filter tokens.
(TokenCollector::visit_items_joined_by_separator): Change store
call.
(TokenCollector::visit_as_line): Likewise.
(TokenCollector::visit_items_as_block): Likewise.
(TokenCollector::trailing_comma): Likewise.
(TokenCollector::visit): Likewise.
(TokenCollector::visit_closure_common): Likewise.
(TokenCollector::visit_function_common): Likewise.
* ast/rust-ast-collector.h (class CollectItem): Add class to
differentiate between formatting and raw data (tokens).
The token collector was using an external container but it was later
revealed that this whole reuse thing was not necessary.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::TokenCollector):
Remove constructor, changing it to an implicit default
constructor.
* ast/rust-ast-collector.h: Change from container reference to
direct container.
* ast/rust-ast-dump.h: Change call to constructor.
* expand/rust-macro-expand.h (struct MacroExpander): Likewise.
gccrs: dump: Output separating space under condition
Separating space shall not be output between every tokens to make the
dump clear and easy to read.
gcc/rust/ChangeLog:
* ast/rust-ast-dump.cc (Dump::require_spacing): Add a function
to determine wether a space shall output.
* ast/rust-ast-dump.h: Add function prototype as well as
condition.
Replace ast dump visitor with call to token collector.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::visit): Add a visit
function for visitable abstract class so we can call a dump on
any visitable item.
* ast/rust-ast-collector.h: Add new visit function prototype.
* ast/rust-ast-dump.cc (Dump::go): Add call to process function.
(Dump::visit): Remove functions.
(Dump::visit_items_joined_by_separator): Likewise.
(Dump::visit_as_line): Likewise.
(Dump::visit_items_as_lines): Likewise.
(Dump::visit_items_as_block): Likewise.
(Dump::visit_function_common): Likewise.
(get_delimiters): Likewise.
(Dump::debug): Update with call to tokencollector.
* ast/rust-ast-dump.h (RUST_AST_DUMP_H): Move include
directives.
(class Dump): Remove multiple prototypes and add generic process
function.
Raiki Tamura [Sun, 18 Jun 2023 14:25:31 +0000 (23:25 +0900)]
gccrs: Refactor lexer to handle UTF-8
gcc/rust/ChangeLog:
* lex/rust-lex.cc (is_float_digit):Change types of param to `uint32_t`
(is_x_digit):Likewise
(is_octal_digit):Likewise
(is_bin_digit):Likewise
(check_valid_float_dot_end):Likewise
(is_whitespace):Likewise
(is_non_decimal_int_literal_separator):Likewise
(is_identifier_start):Likewise
(is_identifier_continue):Likewise
(Lexer::skip_broken_string_input):Likewise
(Lexer::build_token):Remove handling BOM
(Lexer::parse_in_type_suffix):Modify use of `current_char`
(Lexer::parse_in_decimal):Likewise
(Lexer::parse_escape):Likewise
(Lexer::parse_utf8_escape):Likewise
(Lexer::parse_partial_string_continue):Likewise
(Lexer::parse_partial_hex_escape):Likewise
(Lexer::parse_partial_unicode_escape):Likewise
(Lexer::parse_byte_char):Likewise
(Lexer::parse_byte_string):Likewise
(Lexer::parse_raw_byte_string):Likewise
(Lexer::parse_raw_identifier):Likewise
(Lexer::parse_non_decimal_int_literal):Likewise
(Lexer::parse_decimal_int_or_float):Likewise
(Lexer::peek_input):Change return type to `Codepoint`
(Lexer::get_input_codepoint_length):Change to return 1
(Lexer::peek_codepoint_input):Change to be wrapper of `peek_input`
(Lexer::skip_codepoint_input):Change to be wrapper of `skip_input`
(Lexer::test_get_input_codepoint_n_length):Deleted
(Lexer::split_current_token):Deleted
(Lexer::test_peek_codepoint_input):Deleted
(Lexer::start_line):Move backwards
(assert_source_content):New helper function for selftest
(test_buffer_input_source):New helper function for selftest
(test_file_input_source):Likewise
(rust_input_source_test):New test
* lex/rust-lex.h (rust_input_source_test):New test
* rust-lang.cc (run_rust_tests):Add selftest
Philip Herron [Sun, 25 Jun 2023 20:21:36 +0000 (21:21 +0100)]
gccrs: Add method selection to operator overloading
When we do operator overloading we can get multiple candidates and we need
to use the optional arguments to filter the candidates event further. In
the bug we had:
<integer> + <usize>
Without the Add impl blocks for the primitive interger types we unify
against the rhs to figure out that the lhs should be a usize but when we
are using the impl blocks we need to use the rhs to ensure that its
possible to coerce the rhs to the expected fntype parameter to filter the
candidates.
Fixes #2304
gcc/rust/ChangeLog:
* typecheck/rust-autoderef.cc: use new selection filter
* typecheck/rust-hir-dot-operator.cc (MethodResolver::Select): new slection Filter
* typecheck/rust-hir-dot-operator.h: New select prototype
* typecheck/rust-hir-type-check-expr.cc: call select
* typecheck/rust-type-util.cc (try_coercion): new helper
* typecheck/rust-type-util.h (try_coercion): helper prototype
gcc/testsuite/ChangeLog:
* rust/compile/issue-2304.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Change some argument name, as well as some documentation.
gcc/rust/ChangeLog:
* ast/rust-ast.cc (BlockExpr::normalize_tail_expr): Refactor
code and warn about dangling reference.
* expand/rust-expand-visitor.cc (expand_stmt_attribute):
Document function and change argument names to make those
clearer.
The second argument could be derived from the first one, the second
argument is hence useless as the first argument alone could be kept to
access it's data.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc (ExpandVisitor::expand_inner_stmts):
Remove second argument.
(ExpandVisitor::visit): Remove argument from function call.
* expand/rust-expand-visitor.h: Update function prototype.
gccrs: expand: Convert last statement to tail expr
Some inner statements may be expanded to statements as well as a tail
expression, which should then be propagated to the parent tail
expression field.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Change
call to expand_inner_stmts.
(expand_tail_expr): Change argument name and try to expand the
last statement as a tail expression once all statements have
been expanded.
* expand/rust-expand-visitor.h: Change prototype to accept
parent class.
* ast/rust-expr.h: Add try_convert_last_stmt function prototype.
Tail expression may contain attribute and thus should be expanded.
gcc/rust/ChangeLog:
* ast/rust-expr.h: Add a function to take tail expr as well
as a function to set a tail expression.
* expand/rust-expand-visitor.cc (expand_tail_expr): Add tail
expression expansion function.
(ExpandVisitor::visit): Add call to tail expr expansion in for
BlockExpr.
Philip Herron [Tue, 20 Jun 2023 18:41:42 +0000 (19:41 +0100)]
gccrs: Apply generic arguments to the respective trait bounds
When we have an impl block for a generic type such as T which is a generic
type which does not 'bind' generic arguments, which means its not a type
such as an ADT or Fn which holds generic parameter mappings we need to
ensure inference variables are applied to the segment type apropriately so
that inference variables unified correctly and the higher ranked trait
bounds are as well.
Fixes: #1893
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::ResolveImplBlockSelfWithInference):
arguments mappings as an out parameter and apply them to the bounds
* typecheck/rust-hir-type-check-item.h: update the prototype
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments):
apply the arguments to the segment type
gcc/testsuite/ChangeLog:
* rust/compile/issue-1893.rs: fully compile the test case
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Tue, 20 Jun 2023 15:39:25 +0000 (16:39 +0100)]
gccrs: Fix generic argument tracking
When we do generic argument substitution we creating mappings of the
HIR::GenericArgs argument to the TyTy::SubstitutionParam as a pointer. So
for example when we have Generic Parameters <Self, T> and arguments <T, U>
T0: Arguments: <Self=T, T=U>
T1: Self -> replaced-with T
T2: Arguments: <T=T, T=U>
T3: T maps back to the replace Self->T
T4: Arguments <T=T, T=T>
Which is wrong but because we do a string comparison to find the argument
mapping we cant reply on the pointer to the origin parameter mapping as
the parameter will be updated resulting in bad mappings.
This patch changes the Argument mappings to track the _original_ parameter
type so that lookup's for the mappings use this symbol instead not the
updated ones during substitution.
Addresses #1893
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments):
simplify lookup of the respective predicate
* typecheck/rust-tyty-subst.cc (SubstitutionArg::SubstitutionArg): track original parameter
(SubstitutionArg::operator=): update copy ctor
(SubstitutionArg::get_param_ty): use original param
(SubstitutionArg::as_string): update as_string
* typecheck/rust-tyty-subst.h: add new private field
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Tue, 20 Jun 2023 11:21:02 +0000 (12:21 +0100)]
gccrs: add new method to infer impl block type
When we resolve a path segment the associated impl Self may not be a type
which 'binds' generic types. Which would be any type which does not inherit
SubstitionRef base class. There is a case where an impl block for generic
type T has bound generics on the trait bound of the trait impl block this
allows us to infer the T for this case.
Addresses #1893
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::ResolveImplBlockSelfWithInference):
New helper utility to infer non concrete types and handle the case where its not a
binding generic type (which inherits SubstitutionRef)
* typecheck/rust-hir-type-check-item.h: new helper prototype
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): use this helper
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Matthew Jasper [Thu, 8 Jun 2023 19:14:47 +0000 (20:14 +0100)]
gccrs: Parse semicolons in more cases for statement macros
gccrs: Parse statement macros as statements.
gcc/rust/ChangeLog:
* ast/rust-ast.h (MacroInvocation::add_semicolon): New method.
(Expr::to_stmt): Remove method.
* ast/rust-macro.h (MacroInvocation::add_semicolon): Add override.
(MacroInvocation::to_stmt): Remove override.
* ast/rust-stmt.h: Remove use of Expr::to_stmt.
(ExprStmt::add_semicolon): Add override.
* expand/rust-macro-expand.h (struct MacroExpander):
Add EXPR/STMT and remove BLOCK from ContextType.
* expand/rust-expand-visitor.cc (ExpandVisitor::maybe_expand_expr): Use EXPR context.
(ExpandVisitor::expand_inner_stmts): Use STMT context.
(ExpandVisitor::visitor): Remove use of BLOCK context.
* expand/rust-macro-expand.cc (parse_many): Pass enum by value.
(transcribe_on_delimiter): Remove function.
(transcribe_context): Use EXPR/STMT contexts.
(MacroExpander::parse_proc_macro_output): Use EXPR/STMT contexts.
(transcribe_many_stmts): Parse statements with semicolons.
* parse/rust-parse-impl.h (Parser::parse_stmt):
Delegate macro parsing to parse_expr_stmt, check for ! after macro_rules.
(Parser::parse_let_stmt): Work around lack of NT tokens.
(Parser::parse_expr_stmt): Handle statements at end of macro expansions.
(Parser::parse_expr_stmt): Parse macro statements/expression statements
starting with a macro.
(Parser::parse_match_expr): Don't modify flag unnecessarily.
(Parser::parse_stmt_or_expr):
Parse macro statements/expression statements starting with a macro.
(Parser::parse_path_based_stmt_or_expr): Remove method.
(Parser::parse_macro_invocation_maybe_semi): Remove method.
(Parser::parse_expr): Move code into left_denotations.
(Parser::left_denotations): New method.
(Parser::null_denotation): Split out methods for cases with and without paths.
(Parser::null_denotation_path): New method.
(Parser::null_denotation_not_path): New method.
(Parser::parse_macro_invocation_partial): Don't check for semicolon here.
* parse/rust-parse.h: Update declarations.
(struct ParseRestrictions): Additional flag.
gcc/testsuite/ChangeLog:
* rust/compile/braced_macro_arm.rs: New test.
* rust/compile/braced_macro_statements1.rs: New test.
* rust/compile/braced_macro_statements2.rs: New test.
* rust/compile/braced_macro_statements3.rs: New test.
* rust/compile/issue-2225.rs: Update test.
* rust/compile/macro53.rs: New test.
Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
Matthew Jasper [Thu, 8 Jun 2023 18:29:44 +0000 (19:29 +0100)]
gccrs: Handle tail expression normalization right before lowering to HIR.
This allows braced macros at the end of blocks to correctly expand to
zero or more statements followed by a tail expression. Parsing still
creates a tail expression for now.
gcc/rust/ChangeLog:
* ast/rust-ast.cc (BlockExpr::strip_tail_expr):
Don't normalize tail expression in this method.
(BlockExpr::normalize_tail_expr): New method that only does the normalization.
* ast/rust-expr.h: Declare new method.
* hir/rust-ast-lower-block.h: Normalize tail expressions on blocks before lowering.
Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
Raiki Tamura [Mon, 19 Jun 2023 09:06:11 +0000 (18:06 +0900)]
gccrs: tokenize Unicode identifiers
gcc/rust/ChangeLog:
* lex/rust-lex.cc (is_whitespace):add all lacked codepoints valid as whitespaces
(is_identifier_start):new function to check XID_Start and underscore
(is_identifier_continue):new function to check XID_Continue
(Lexer::build_token):tokenize Unicode identifiers
(Lexer::parse_partial_string_continue):add comments
(Lexer::parse_partial_unicode_escape):add comments
(Lexer::parse_raw_identifier):change to use `is_identifier_scontinue`
(Lexer::parse_identifier_or_keyword):change to use `is_identifier_continue`
(Lexer::parse_char_or_lifetime):change to use `is_identifier_start/continue`
(Lexer::skip_codepoint_input):do not attempt to skip input when bumping EOF
* lex/rust-lex.h:add `current_char32` field
Philip Herron [Wed, 14 Jun 2023 11:11:38 +0000 (12:11 +0100)]
gccrs: we can't check the bounds involving empty placeholder types
We use placeholders for assoicated types on traits but if we are unifying
types against a placeholder its not possible to check the bounds as the
placeholder does not have enough information yet at this point to determine
if bounds will or won't be satisfied. That check will occur when associated
types and generics are setup.
Fixes #2036
gcc/rust/ChangeLog:
* typecheck/rust-unify.cc (UnifyRules::go): dont check bounds on placeholders
gcc/testsuite/ChangeLog:
* rust/compile/issue-2036.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>