Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

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

@CohenArthur CohenArthur requested a review from philberty March 5, 2025 15:45
@CohenArthur CohenArthur force-pushed the tychk-let-else-diverge branch from 3d1e2a6 to 2ab175c Compare March 24, 2025 13:27
gcc/rust/ChangeLog:

	* ast/rust-stmt.h (class LetStmt): Add optional expression for diverging else.
	* ast/rust-ast-builder.cc (Builder::let): Use new API.
gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_let_stmt): Add new parsing in case of `else` token.
gcc/rust/ChangeLog:

	* ast/rust-ast-collector.cc (TokenCollector::visit): Add handling for diverging else
	expression.
gcc/rust/ChangeLog:

	* resolve/rust-ast-resolve-stmt.h: Add handling for diverging else.
	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Likewise.
gcc/rust/ChangeLog:

	* hir/tree/rust-hir-stmt.h (class LetStmt): Add optional diverging else expression.
	* hir/tree/rust-hir-stmt.cc: Likewise.
	* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Add handling for lowering
	diverging else.
@CohenArthur CohenArthur force-pushed the tychk-let-else-diverge branch from 2ab175c to cfd49f3 Compare March 24, 2025 16:49
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants