Skip to content

gccrs: Fix cast rules logic to try simple casts then fall back to coercions#3900

Merged
philberty merged 1 commit into
masterfrom
phil/casts-fix
Jul 10, 2025
Merged

gccrs: Fix cast rules logic to try simple casts then fall back to coercions#3900
philberty merged 1 commit into
masterfrom
phil/casts-fix

Conversation

@philberty

@philberty philberty commented Jul 10, 2025

Copy link
Copy Markdown
Member

This case:

let i = 1;
let j = i as i64;

'i' is meant to default to i32 but the inference was making both of these i64 because the code was prefering coercion logic which can end up with a default unify which causes the ?integer to unify with i64 making them both i64.

But all we need to do is allow the simple cast rules to run first then fallback to coercions but special consideration has to be made to ensure that if there are dyn objects needed then this needs a unsize coercion, but also we need to ensure the underlying types are a valid simple cast too otherwise these also need to fallback to the coercion code.

Fixes #2680

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::resolve): optional emit_error flag
(TypeCastRules::check): try the simple cast rules then fallback to coercions
(TypeCastRules::check_ptr_ptr_cast): ensure the underlying's
(TypeCastRules::emit_cast_error): make this a static helper
* typecheck/rust-casts.h: new emit_error prototype

gcc/testsuite/ChangeLog:

* rust/compile/issue-2680.rs: New test.

…rcions

This case:

    let i = 1;
    let j = i as i64;

'i' is meant to default to i32 but the inference was making both of these
i64 because the code was prefering coercion logic which can end up with a
default unify which causes the ?integer to unify with i64 making them both
i64.

But all we need to do is allow the simple cast rules to run first then
fallback to coercions but special consideration has to be made to ensure
that if there are dyn objects needed then this needs a unsize coercion, but
also we need to ensure the underlying types are a valid simple cast too
otherwise these also need to fallback to the coercion code.

Fixes #2680

gcc/rust/ChangeLog:

	* typecheck/rust-casts.cc (TypeCastRules::resolve): optional emit_error flag
	(TypeCastRules::check): try the simple cast rules then fallback to coercions
	(TypeCastRules::check_ptr_ptr_cast): ensure the underlying's
	(TypeCastRules::emit_cast_error): make this a static helper
	* typecheck/rust-casts.h: new emit_error prototype

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2680.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
@philberty philberty changed the title gccrs: Fix cast rules logic to try simple casts then fall back to coe… gccrs: Fix cast rules logic to try simple casts then fall back to coercions Jul 10, 2025
@philberty philberty added this pull request to the merge queue Jul 10, 2025
Merged via the queue into master with commit 8f23ec6 Jul 10, 2025
13 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in libcore 1.49 Jul 10, 2025
@philberty philberty deleted the phil/casts-fix branch July 11, 2025 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Different inference than rustc for integer

1 participant