Skip to content

Comments

Typecheck else expression of let-else to ensure it diverges#3469

Open
CohenArthur wants to merge 2 commits intoRust-GCC:masterfrom
CohenArthur:tychk-let-else-diverge
Open

Typecheck else expression of let-else to ensure it diverges#3469
CohenArthur wants to merge 2 commits intoRust-GCC:masterfrom
CohenArthur:tychk-let-else-diverge

Conversation

@CohenArthur
Copy link
Member

@CohenArthur CohenArthur commented Mar 5, 2025

The else expression of a let-else must always diverge, which we enforce during typecheck. Needs #3468

Comment on lines +115 to +116
// FIXME: Is that enough? Do we need to do something like
// `append_reference` here as well?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philberty I'm not sure if there's anything else we need to do here. I was trying to use something like coercion_site(...) but obviously it doesn't work because any type is coercible to !, which is not the behavior we actually want here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

! should be coercible to any other type, but not the other way around

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok yeah this is enough because all you need to do is check for ! in the else case so yes this is correct! Dont need to do unify or coercion site because it doesnt need to do anything else.

Dont think we support this in codegen

@CohenArthur CohenArthur requested a review from philberty March 5, 2025 15:45
@CohenArthur CohenArthur force-pushed the tychk-let-else-diverge branch 2 times, most recently from 2ab175c to cfd49f3 Compare March 24, 2025 16:49
Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM this is enough

gcc/rust/ChangeLog:

	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Visit the `else` expression instead
	of the `init` one if the let is a let-else.
Make sure the `else` expression resolves to the never type.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit): Check diverging else
	against `!`.

gcc/testsuite/ChangeLog:

	* rust/compile/let-else-invalid-type.rs: New test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants