Repository navigation
Fix check_expr_if to point to a more accurate location of the compilation error in some cases #147484
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
sgasho
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
sgasho:fix/141690_check_expr_if
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix check_expr_if to point to a more accurate location of the compilation error in some cases #147484
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| use rustc_hir as hir; | ||
| use rustc_hir::{ExprKind, HirId}; | ||
| use rustc_middle::ty::{Ty, TypeVisitableExt}; | ||
| use rustc_span::Span; | ||
| use rustc_span::def_id::LocalDefId; | ||
|
|
||
| use crate::Expectation; | ||
| use crate::coercion::CoerceMany; | ||
| use crate::diverges::Diverges; | ||
| use crate::fn_ctxt::FnCtxt; | ||
|
|
||
| /// State shared across all branches of an `if`/`else if` chain check. | ||
| struct IfChainCx<'a, 'tcx> { | ||
| coerce: &'a mut CoerceMany<'tcx>, | ||
| outer_if_expr_id: HirId, | ||
| orig_expected: Expectation<'tcx>, | ||
| tail_defines_return_position_impl_trait: Option<LocalDefId>, | ||
| } | ||
|
|
||
| impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | ||
| // A generic function for checking the 'then' and 'else' clauses in an 'if' | ||
| // or 'if-else' expression. | ||
| pub(crate) fn check_expr_if( | ||
| &self, | ||
| expr_id: HirId, | ||
| cond_expr: &'tcx hir::Expr<'tcx>, | ||
| then_expr: &'tcx hir::Expr<'tcx>, | ||
| opt_else_expr: Option<&'tcx hir::Expr<'tcx>>, | ||
| sp: Span, | ||
| orig_expected: Expectation<'tcx>, | ||
| ) -> Ty<'tcx> { | ||
| let expected = orig_expected.try_structurally_resolve_and_adjust_for_branches(self); | ||
|
|
||
| let coerce_to_ty = expected.coercion_target_type(self, sp); | ||
| let mut coerce = CoerceMany::with_capacity(coerce_to_ty, 2); | ||
|
|
||
| let mut cx = IfChainCx { | ||
| coerce: &mut coerce, | ||
| outer_if_expr_id: expr_id, | ||
| orig_expected, | ||
| tail_defines_return_position_impl_trait: self | ||
| .return_position_impl_trait_from_match_expectation(orig_expected), | ||
| }; | ||
| let branch_ty = | ||
| self.check_if_branch(&mut cx, None, cond_expr, then_expr, opt_else_expr, sp); | ||
|
|
||
| let result_ty = coerce.complete(self); | ||
| if let Err(guar) = branch_ty.error_reported() { | ||
| Ty::new_error(self.tcx, guar) | ||
| } else { | ||
| result_ty | ||
| } | ||
| } | ||
|
|
||
| /// Checks one `if` of an `if`/`else if` chain and recurses into the next `else if`. | ||
| /// | ||
| /// `prev_branch` is `None` for the outermost `if`. | ||
| /// Every later branch is coerced against the preceding one (`prev_branch`), | ||
| /// so a mismatch is reported at that branch rather than at the whole `else` chain. | ||
| fn check_if_branch( | ||
| &self, | ||
| cx: &mut IfChainCx<'_, 'tcx>, | ||
| prev_branch: Option<HirId>, | ||
| cond_expr: &'tcx hir::Expr<'tcx>, | ||
| then_expr: &'tcx hir::Expr<'tcx>, | ||
| opt_else_expr: Option<&'tcx hir::Expr<'tcx>>, | ||
| sp: Span, | ||
| ) -> Ty<'tcx> { | ||
| let cond_ty = self.check_expr_has_type_or_error(cond_expr, self.tcx.types.bool, |_| {}); | ||
|
|
||
| self.warn_if_unreachable( | ||
| cond_expr.hir_id, | ||
| then_expr.span, | ||
| "block in `if` or `while` expression", | ||
| ); | ||
|
|
||
| let cond_diverges = self.diverges.get(); | ||
| self.diverges.set(Diverges::Maybe); | ||
|
|
||
| let expected = cx.orig_expected.try_structurally_resolve_and_adjust_for_branches(self); | ||
| let then_ty = self.check_expr_with_expectation(then_expr, expected); | ||
| let then_diverges = self.diverges.get(); | ||
| self.diverges.set(Diverges::Maybe); | ||
|
|
||
| if let Some(prev_hir_id) = prev_branch { | ||
| let cause = self.if_cause( | ||
| cx.outer_if_expr_id, | ||
| prev_hir_id, | ||
| then_expr.hir_id, | ||
| cx.tail_defines_return_position_impl_trait, | ||
| ); | ||
| cx.coerce.coerce(self, &cause, then_expr, then_ty); | ||
| } else { | ||
| cx.coerce.coerce(self, &self.misc(sp), then_expr, then_ty); | ||
| } | ||
|
|
||
| match opt_else_expr { | ||
| Some(else_expr) | ||
| if let ExprKind::If(next_cond_expr, next_then_expr, next_opt_else_expr) = | ||
| else_expr.kind => | ||
| { | ||
| self.check_expr_with_check_fn(else_expr, expected, |this| { | ||
| this.check_if_branch( | ||
| cx, | ||
| Some(then_expr.hir_id), | ||
| next_cond_expr, | ||
| next_then_expr, | ||
| next_opt_else_expr, | ||
| else_expr.span, | ||
| ) | ||
| }); | ||
|
|
||
| let else_diverges = self.diverges.get(); | ||
| self.diverges.set(cond_diverges | then_diverges & else_diverges); | ||
| } | ||
| Some(else_expr) => { | ||
| let else_ty = self.check_expr_with_expectation(else_expr, expected); | ||
| let else_diverges = self.diverges.get(); | ||
| let cause = self.if_cause( | ||
| cx.outer_if_expr_id, | ||
| then_expr.hir_id, | ||
| else_expr.hir_id, | ||
| cx.tail_defines_return_position_impl_trait, | ||
| ); | ||
| cx.coerce.coerce(self, &cause, else_expr, else_ty); | ||
| self.diverges.set(cond_diverges | then_diverges & else_diverges); | ||
| } | ||
| None => { | ||
| self.if_fallback_coercion(sp, cond_expr, then_expr, cx.coerce); | ||
|
|
||
| self.diverges.set(cond_diverges); | ||
| } | ||
| }; | ||
|
|
||
| if let Err(guar) = cond_ty.error_reported() { | ||
| Ty::new_error(self.tcx, guar) | ||
| } else { | ||
| cx.coerce.merged_ty() | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So,
check_expr_ifandcheck_else_if_branchare very similar, and I wonder if they could be combined - or at least with considerably more code combined.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed: refactor: simplify _if.rs and remove IfCoerceChain