Philip Herron [Fri, 12 Sep 2025 15:37:32 +0000 (16:37 +0100)]
gccrs: Fix ICE with invalid const expression
This patch handles the overflowed var expression in the const block, so that
we error properly in the const expr code. It was missing some stuff from the
c++ implementation in how this should be handled properly.
Fixes Rust-GCC#4139
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (CompileExpr::compile_integer_literal): cleanup
* backend/rust-constexpr.cc (struct constexpr_global_ctx): port over c++ helpers
(decl_really_constant_value): likewise
(eval_constant_expression): likewise
(non_const_var_error): likewise
gcc/testsuite/ChangeLog:
* rust/compile/issue-4139.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Wed, 3 Sep 2025 19:50:59 +0000 (20:50 +0100)]
gccrs: track abi and unsafety on fnptrs for proper type checking
We need to distinguish between abi's and unsafety on fnptrs. There is
a commented out check because there is a regression in:
rust/compile/try-catch-unwind-{new/old}.rs
But i think this is because the test case should be taking an FnOnce
from reading std::panic in rust 1.49. Where as we are passing an fnptr
which is probably because we didnt support fnonce at all then.
Addresses Rust-GCC/gccrs#4090
gcc/rust/ChangeLog:
* hir/tree/rust-hir-item.h: add unsafe helper
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): pass in abi and unsafe
* typecheck/rust-tyty.cc (BaseType::monomorphized_clone): likewise
(FnPtr::as_string): emit more info
(FnPtr::clone): update ctor call
* typecheck/rust-tyty.h: new ctor params
* typecheck/rust-unify.cc (UnifyRules::expect_fnptr): check abi and unsafe
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gccrs: Unify path-to-type handling in Late resolver
gcc/rust/ChangeLog:
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Use
resolve_type_path_like in overloads for TypePath,
StructExprStruct, StructExprStructBase, and
StructExprStructFields.
(resolve_type_path_like): New static function based off
Late::visit overload for TypePath.
* util/rust-unwrap-segment.h (unwrap_segment_error_string): New
inline static function definitions.
Philip Herron [Sun, 31 Aug 2025 21:57:05 +0000 (22:57 +0100)]
gccrs: Fix segv when handling invalid array capacities
We need to catch the error node for the array capacity and return early.
Otherwise we try to const evaluate something thats just silly. Also
when compiling array expressions we can simply reuse the array capacity
expression we already have cons folded.
Fixes Rust-GCC#3965
gcc/rust/ChangeLog:
* backend/rust-compile-context.h: add assertions for context peeks
* backend/rust-compile-expr.cc (CompileExpr::visit): check for valid loop context
(CompileExpr::array_copied_expr): just reuse array tyty capacity value
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): catch error
gcc/testsuite/ChangeLog:
* rust/compile/issue-3965-1.rs: New test.
* rust/compile/issue-3965-2.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
This doesn't actually handle the attribute, although it does allow us to
recognize it as valid.
gcc/rust/ChangeLog:
* util/rust-attribute-values.h
(Attributes::RUSTC_ALLOW_CONST_FN_UNSTABLE): New static
constexpr member variable.
* util/rust-attributes.cc (__definitions): Add entry for
RUSTC_ALLOW_CONST_FN_UNSTABLE.
Philip Herron [Sun, 3 Aug 2025 18:53:23 +0000 (19:53 +0100)]
gccrs: allow unifications against non const types
When type resolving a function which returns a const generic for example
this means its unifying the ConstType vs the the specified type so this
mean unwrapping the type of the const.
gcc/rust/ChangeLog:
* typecheck/rust-unify.cc (UnifyRules::go): unwrap the const type
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Fri, 29 Aug 2025 16:54:54 +0000 (17:54 +0100)]
gccrs: Remove fully the can_eq TyTy::BaseType::can_eq interface
This was an initial helper from back in the day which was not maintained
but was a nice const simple way to check if types are compatable. But
reusing our unify code is much much more acurate and single source of
truth on the type system.
Philip Herron [Fri, 29 Aug 2025 16:42:10 +0000 (17:42 +0100)]
gccrs: simplify cloning of types during unify
We should only clone when we commit and it was successful. With no
cloning there is a regression in min specialization 2. Probably because
it needs to know the unify site hirid and ensure we have it set there.
Philip Herron [Mon, 25 Aug 2025 12:30:22 +0000 (13:30 +0100)]
gccrs: respect the unify rules commit flag
We use the types compatable interface for unify here and so if
we dont respect the commit flag the interface can have unintended
side effects with infer type hints throwing things off down the line.
Philip Herron [Mon, 25 Aug 2025 10:34:25 +0000 (11:34 +0100)]
gccrs: Remove some const usage so we can get rid of more can_eq usage
I needed to remove som usage of const so we can get rid of can_eq in
type bounds probe. This means we can use the types_compatable interface
instead. Which is much better.
Philip Herron [Mon, 25 Aug 2025 19:44:16 +0000 (20:44 +0100)]
gccrs: Make the coerce borrowed pointer consistent with try flag
This interface is not respecting the coercion api commit is always false,
we need to ensure this is respected via the try flag like the rest of the
coercion logic.
gcc/rust/ChangeLog:
* typecheck/rust-coercion.cc (TypeCoercionRules::select): respect try flag
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gccrs: Error out on unsuffixed self list use declaration
gcc/rust/ChangeLog:
* resolve/rust-early-name-resolver-2.0.cc (Early::finalize_rebind_import):
Replace assert with early break and remove early return.
(Early::visit): Check for unsuffixed lower self list.
* resolve/rust-early-name-resolver-2.0.h: Add visit function prototype.
gcc/testsuite/ChangeLog:
* rust/compile/use_self_alone_in_list.rs: New test.
Owen Avery [Sat, 23 Aug 2025 02:26:05 +0000 (22:26 -0400)]
gccrs: nr1.0: Remove support in borrow checker
gcc/rust/ChangeLog:
* checks/errors/borrowck/rust-bir-builder-internal.h: Remove
inclusion of "rust-name-resolver.h".
(BuilderContext::resolver): Change type to nr2.0 resolver.
(BuilderContext::BuilderContext): Change initialization of
resolver reference.
(AbstractBuilder::resolve_label): Assume name resolution 2.0 is
enabled.
(AbstractBuilder::resolve_variable): Likewise.
(AbstractBuilder::resolve_variable_or_fn): Likewise.
Owen Avery [Sat, 23 Aug 2025 02:57:05 +0000 (22:57 -0400)]
gccrs: nr1.0: Remove support in pattern checker
gcc/rust/ChangeLog:
* checks/errors/rust-hir-pattern-analysis.cc: Remove inclusion
of "options.h".
(PatternChecker::PatternChecker): Change initialization of
resolver reference.
(PatternChecker::visit): Assume name resolution 2.0 is enabled.
* checks/errors/rust-hir-pattern-analysis.h: Include nr2.0
header instead of nr1.0 header.
(PatternChecker::resolver): Change type to nr2.0 resolver.
Owen Avery [Sat, 23 Aug 2025 02:12:35 +0000 (22:12 -0400)]
gccrs: nr1.0: Remove support in rust-session-manager.cc
gcc/rust/ChangeLog:
* rust-session-manager.cc: Remove inclusion of name resolution
1.0 headers.
(Session::compile_crate): Assume name resolution 2.0 is enabled.
(Session::expansion): Likewise.
(Session::load_extern_crate): Likewise.
Owen Avery [Sat, 23 Aug 2025 17:29:54 +0000 (13:29 -0400)]
gccrs: nr1.0: Remove support in privacy checker
gcc/rust/ChangeLog:
* checks/errors/privacy/rust-privacy-check.cc: Adjust includes.
(Resolver::resolve): Pass 2.0 name resolution context to
VisibilityResolver and PrivacyReporter.
* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::PrivacyReporter): Change type of resolver
parameter.
(is_child_module): Remove static function.
(PrivacyReporter::check_for_privacy_violation): Assume nr2.0 is
enabled and handle removal of is_child_module.
* checks/errors/privacy/rust-privacy-reporter.h: Adjust
includes.
(PrivacyReporter::PrivacyReporter): Change type of resolver
parameter.
(PrivacyReporter::resolver): Change member variable type.
* checks/errors/privacy/rust-visibility-resolver.cc: Adjust
includes.
(VisibilityResolver::VisibilityResolver): Change type of
resolver parameter.
(VisibilityResolver::resolve_module_path): Assume nr2.0 is
enabled.
* checks/errors/privacy/rust-visibility-resolver.h: Adjust
includes.
(VisibilityResolver::VisibilityResolver): Change type of
resolver parameter.
(VisibilityResolver::resolver): Change member variable type.
gccrs: Refactor AST Patterns that hold multiple pattern items
Patterns include TuplePattern, TupleStructPattern & SlicePattern. Besides making their pattern
items container class inherit from a common `PatternItems` base class just like in HIR for
better standardization, mentions of `Range` are also changed to `HasRest` or `NoRest`.
gcc/rust/ChangeLog:
* ast/rust-pattern.h:
- Add a new base abstract class `PatternItems` which are used by pattern items class
class derivatives for `TuplePattern`, `TupleStructPattern` & `SlicePattern`.
- Standardized the derived class names to have `HasRest` or `NoRest` as suffixes.
- Values for the common `ItemType` enum is updated to `HAS_REST` or `NO_REST`.
* ast/rust-pattern.cc: Renamed the classes accordingly.
* ast/rust-ast-collector.cc: Renamed the classes accordingly.
* ast/rust-ast-collector.h: Renamed the classes accordingly.
* ast/rust-ast-full-decls.h: Renamed the classes accordingly.
* ast/rust-ast-visitor.cc: Renamed the classes accordingly.
* ast/rust-ast-visitor.h: Renamed the classes accordingly.
* ast/rust-desugar-for-loops.cc: Renamed the classes accordingly.
* ast/rust-desugar-question-mark.cc: Renamed the classes accordingly.
* expand/rust-cfg-strip.cc: Renamed the classes accordingly.
* expand/rust-cfg-strip.h: Renamed the classes accordingly.
* expand/rust-derive-clone.cc: Renamed the classes accordingly.
* expand/rust-derive-cmp-common.cc: Renamed the classes accordingly.
* expand/rust-derive-hash.cc: Renamed the classes accordingly.
* expand/rust-derive-ord.cc: Renamed the classes accordingly.
* expand/rust-derive-partial-eq.cc: Renamed the classes accordingly.
* expand/rust-derive.h: Renamed the classes accordingly.
* expand/rust-expand-visitor.cc: Renamed the classes accordingly.
* expand/rust-expand-visitor.h: Renamed the classes accordingly.
* hir/rust-ast-lower-base.cc: Renamed the classes accordingly.
* hir/rust-ast-lower-base.h: Renamed the classes accordingly.
* hir/rust-ast-lower-pattern.cc: Renamed the classes accordingly.
* hir/tree/rust-hir-pattern.h: Renamed the classes accordingly.
* parse/rust-parse-impl.h: Renamed the classes accordingly.
* resolve/rust-ast-resolve-base.cc: Renamed the classes accordingly.
* resolve/rust-ast-resolve-base.h: Renamed the classes accordingly.
* resolve/rust-ast-resolve-pattern.cc: Renamed the classes accordingly.
* util/rust-attributes.cc: Renamed the classes accordingly.
* util/rust-attributes.h: Renamed the classes accordingly.
Owen Avery [Sun, 24 Aug 2025 23:37:45 +0000 (19:37 -0400)]
gccrs: Avoid std::unique_ptr<std::string> in Token
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::visit): Use
should_have_str instead of has_str.
* ast/rust-ast.cc (Token::as_string): Likewise.
* expand/rust-macro-builtins-offset-of.cc
(MacroBuiltin::offset_of_handler): Likewise.
* lex/rust-lex.cc (Lexer::dump_and_skip): Likewise.
* ast/rust-ast.h (class Token): Remove dead code.
(Token::has_str): Remove function.
(Token::should_have_str): New function.
* lex/rust-token.cc (Token::get_str): Remove function
definition.
* lex/rust-token.h: Include "rust-diagnostics.h".
(Token::str): Change type from std::unique_ptr<std::string> to
std::string.
(Token::Token): Adjust initialization of str member variable and
the type of the parameter used to initialize it.
(Token::make_identifier): Accept std::string instead of rvalue
reference to std::string.
(Token::make_int): Likewise.
(Token::make_float): Likewise.
(Token::make_string): Likewise.
(Token::make_byte_string): Likewise.
(Token::make_raw_string): Likewise.
(Token::make_inner_doc_comment): Likewise.
(Token::make_outer_doc_comment): Likewise.
(Token::make_lifetime): Likewise.
(Token::get_str): Add definition to function declaration.
(Token::has_str): Remove member function.
(Token::should_have_str): Handle INNER_DOC_COMMENT and
OUTER_DOC_COMMENT.
Owen Avery [Fri, 22 Aug 2025 02:22:36 +0000 (22:22 -0400)]
gccrs: Eagerly expand format_args macro
This allows format_args!(some_macro!(...), ...) to compile.
gcc/rust/ChangeLog:
* expand/rust-macro-builtins-format-args.cc
(format_args_parse_arguments): Split format expression parsing
into...
(format_args_parse_expr): ...a new function here, while handling
eager expansion.
(MacroBuiltin::format_args_handler): Use format_args_parse_expr.
This improves our handling of PathInExpression and fixes
https://github.com/Rust-GCC/gccrs/issues/4056.
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::parse_expr): Avoid skipping
or splitting tokens.
(Parser::null_denotation): Assume initial token was not skipped
and adjust function signature to match. Use
parse_path_in_expression instead of
parse_path_in_expression_pratt and handle SCOPE_RESOLUTION.
(Parser::parse_path_in_expression_pratt): Remove function.
* parse/rust-parse.h (null_denotation): Remove initial token
parameter.
(parse_path_in_expression_pratt): Remove function.
Error kind were split between error state and out bool parameter.
gcc/rust/ChangeLog:
* typecheck/rust-coercion.cc (TypeCoercionRules::do_coercion): Adapt
call to new return type.
(TypeCoercionRules::coerce_unsized): Change error handling.
* typecheck/rust-coercion.h: Update function prototype and add new
CoerceUnsizedError error type.
gccrs: Implement rest pattern support for slice patterns
This patch adds support for compiling rest patterns present in slice patterns
(e.g. `[1, .., 2]`). 006t.original output from compiling match-slicepattern-slice-2.rs
for first 2 match arms:
* hir/tree/rust-hir-pattern.h: Add SlicePatternItems base class and
SlicePatternItemsNoRest/SlicePatternItemsHasRest derived classes.
Update SlicePattern to hold patterns using the new classes.
* hir/tree/rust-hir-full-decls.h: Declare new classes.
* hir/tree/rust-hir.cc: Add visits for new classes.
* hir/tree/rust-hir-visitor.h: Add visits for new classes.
* hir/tree/rust-hir-visitor.cc: Implement visits for new classes.
* hir/rust-hir-dump.h: Add visits for new classes.
* hir/rust-hir-dump.cc: Implement Dump::visit for new classes.
* hir/rust-ast-lower-base.h: Declare new lower_slice_pattern_no_rest/has_rest
methods.
* hir/rust-ast-lower-base.cc: Implement lower_slice_pattern_no_rest/has_rest
to lower AST slice pattern items to HIR.
* hir/rust-ast-lower-pattern.cc: Update ASTLoweringPattern::visit for
SlicePattern to use new lowering methods.
* backend/rust-compile-pattern.cc: Update CompilePatternCheckExpr::visit
and CompilePatternBindings::visit for SlicePattern to handle
SlicePatternItemsNoRest/HasRest.
* checks/errors/borrowck/rust-bir-builder-pattern.cc: Update
PatternBindingBuilder::visit for SlicePattern to iterate members correctly.
* checks/errors/borrowck/rust-bir-builder-struct.h: Add visits for new
classes.
* checks/errors/borrowck/rust-function-collector.h: Add visits for new
classes.
* checks/errors/rust-const-checker.h: Add visits for new classes.
* checks/errors/rust-const-checker.cc: Implement empty visits for new classes.
* checks/errors/rust-hir-pattern-analysis.h: Add visits for new classes.
* checks/errors/rust-hir-pattern-analysis.cc: Implement empty visits for new
classes.
* checks/errors/rust-unsafe-checker.h: Add visits for new classes.
* checks/errors/rust-unsafe-checker.cc: Implement empty visits for new
classes.
* typecheck/rust-hir-type-check-pattern.cc: Update TypeCheckPattern::visit
for SlicePattern to handle SlicePatternItemsNoRest/HasRest.
Ryutaro Okada [Tue, 12 Aug 2025 05:17:09 +0000 (22:17 -0700)]
gccrs: Replace old read-only checker with new implementation.
gcc/rust/ChangeLog:
* Make-lang.in: Replace old read-only checker with new implementation.
* checks/errors/rust-readonly-check.cc (ReadonlyChecker::ReadonlyChecker):
Replace old read-only checker with new implementation.
* checks/errors/rust-readonly-check.h:
Replace old read-only checker with new implementation.
* rust-session-manager.cc (Session::compile_crate):
Switch to new read-only checker.
* checks/errors/rust-readonly-check2.cc: Removed.
* checks/errors/rust-readonly-check2.h: Removed.
Ryutaro Okada [Tue, 12 Aug 2025 05:01:08 +0000 (22:01 -0700)]
gccrs: Read-only check if the variable is mutable type.
gcc/rust/ChangeLog:
* checks/errors/rust-readonly-check2.cc (ReadonlyChecker::check_variable):
Read-only check if the variable is mutable type.
(ReadonlyChecker::is_mutable_type): Read-only check if the variable is mutable type.
* checks/errors/rust-readonly-check2.h: Read-only check if the variable is mutable type.
* backend/rust-compile-pattern.cc(CompilePatternLet::visit(IdentifierPattern)):
Add support for subpatterns.
* backend/rust-compile-var-decl.h(CompileVarDecl::visit(IdentifierPattern)):
Implement compilation for subpatterns.
lishin [Wed, 13 Aug 2025 18:16:52 +0000 (19:16 +0100)]
gccrs: fix ICE by skipping invalid (non-FNDEF) candidates
gcc/rust/ChangeLog:
* typecheck/rust-hir-dot-operator.cc (MethodResolver::Select):
Skip asserts by checking candidate type and using early-continue.
(MethodResolver::try_select_predicate_candidates):
Skip invalid candidates.
Owen Avery [Mon, 11 Aug 2025 17:44:25 +0000 (13:44 -0400)]
gccrs: Improve handling of non-final path segments
gcc/rust/ChangeLog:
* resolve/rust-forever-stack.hxx
(ForeverStack::resolve_segments): Remove usage of optional
reference, allow non-final path segments to resolve to types
even outside the type namespace, and allow resolution to
progress past non-final path segments which resolve to modules.
No check was performed and the value was assumed non null.
Path conversion returned an empty path for "Self" due to a hack in
generics Self injection that prevented any "Self!" macro from beeing
recognized correctly.
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::parse_stmt_or_expr): Add null check
on parse_macro_invocation_partial call.
* ast/rust-path.cc (Path::convert_to_simple_path): Do not exclude
capitalized "Self".
Arthur Cohen [Tue, 12 Aug 2025 15:04:03 +0000 (17:04 +0200)]
gccrs: ast: Cleanup SingleASTNode::NodeType
gcc/rust/ChangeLog:
* ast/rust-ast.h: Change NodeType to enum class Kind.
* ast/rust-ast-fragment.cc: Use new names.
* ast/rust-ast-fragment.h: Likewise.
* ast/rust-ast.cc (SingleASTNode::SingleASTNode): Likewise.
Owen Avery [Tue, 12 Aug 2025 02:06:02 +0000 (22:06 -0400)]
gccrs: CfgStrip AST nodes marked with #[test]
gcc/rust/ChangeLog:
* expand/rust-cfg-strip.cc: Include "rust-macro-expand.h".
(fails_cfg): Rename to...
(CfgStrip::fails_cfg): ...here and handle test attributes.
(fails_cfg_with_expand): Rename to...
(CfgStrip::fails_cfg_with_expand): ...here and handle test
attributes.
* expand/rust-cfg-strip.h (struct ExpansionCfg): Forward
declare.
(CfgStrip::fails_cfg): New member function.
(CfgStrip::fails_cfg_with_expand): Likewise.
(CfgStrip::CfgStrip): Accept reference to ExpansionCfg.
(CfgStrip::expansion_cfg): New member variable.
* rust-session-manager.cc (Session::expansion): Pass
ExpansionCfg instance to CfgStrip constructor.
gccrs: Emit an error message on unsupported llvm_asm
llvm_asm was never meant to be completely supported since it has been
replaced with the asm macro but we still need it to compile some parts
of libcore, previously the compiler was aborting when an unsupported
llvm_asm construct was found.
gcc/rust/ChangeLog:
* ast/rust-expr.h: Add const getters to llvm members.
* hir/rust-ast-lower-expr.cc (check_llvm_asm_support): Check llvm_asm
usage validity.
(ASTLoweringExpr::visit): Emit an error message instead of aborting.
Owen Avery [Sat, 9 Aug 2025 22:33:58 +0000 (18:33 -0400)]
gccrs: Add checks to ExpandVisitor
This should help detect issues like
https://github.com/Rust-GCC/gccrs/issues/3444.
gcc/rust/ChangeLog:
* ast/rust-ast.h (Stmt::get_node_id): Make virtual.
(Type::get_node_id): Likewise.
(AssociatedItem::get_node_id): New virtual member function.
* ast/rust-expr.h (TypeCastExpr::get_casted_expr_ptr): New
member function.
(TypeCastExpr::get_type_to_cast_to_ptr): Likewise.
(ClosureExprInner::get_definition_expr_ptr): Likewise.
* ast/rust-item.h (TypeAlias::get_node_id): New member function
to override AssociatedItem::get_node_id.
(ConstantItem::get_node_id): Likewise.
* expand/rust-expand-visitor.cc
(ExpandVisitor::maybe_expand_expr): Adjust
macro_invoc_expect_id.
(ExpandVisitor::maybe_expand_type): Likewise and add an overload
for std::unique_ptr<TypeNoBounds>.
(ExpandVisitor::visit): Check macro_invoc_expect_id and
generally improve visitors so that the testsuite will still
pass.
* expand/rust-expand-visitor.h (ExpandVisitor::ExpandVisitor):
Initialize member variable macro_invoc_expect_id.
(ExpandVisitor::maybe_expand_type): Add an overload for
std::unique_ptr<TypeNoBounds>.
(ExpandVisitor::expand_macro_children): Adjust
macro_invoc_expect_id.
(ExpandVisitor::visit): Add an overload for TypeCastExpr.
(ExpandVisitor::macro_invoc_expect_id): New member variable.
gcc/testsuite/ChangeLog:
* rust/compile/macros/mbe/macro49.rs: Add missing lang items.
lishin [Fri, 8 Aug 2025 21:35:20 +0000 (22:35 +0100)]
gccrs: Fix ICE on exclusive_range_pattern lowering
gcc/rust/ChangeLog:
* backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit):
Check upper compare operator.
* hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::visit):
Handle lowering of exclusive range pattern.
* hir/tree/rust-hir-pattern.h (class RangePattern):
Add support for exclusive ranges in HIR representation.
* expand/rust-macro-expand.cc (transcribe_expression): Parse any
outer attributes before parsing an expression.
* parse/rust-parse.h (Parser::parse_outer_attributes): Make
public.
Several place built an object before copying it in a vector. This commit
favorise in place construction and use readily available vector size to
reserve memory beforehand.