Skip to content
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

Ignore ExprKind::DropTemps for some ref suggestions #65213

Merged
merged 3 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,23 @@ impl Expr {
}
}
}

/// If `Self.kind` is `ExprKind::DropTemps(expr)`, drill down until we get a non-`DropTemps`
/// `Expr`. This is used in suggestions to ignore this `ExprKind` as it is semantically
/// silent, only signaling the ownership system. By doing this, suggestions that check the
/// `ExprKind` of any given `Expr` for presentation don't have to care about `DropTemps`
/// beyond remembering to call this function before doing analysis on it.
pub fn peel_drop_temps(&self) -> &Self {
let mut base_expr = self;
loop {
Centril marked this conversation as resolved.
Show resolved Hide resolved
match &base_expr.kind {
ExprKind::DropTemps(expr) => {
base_expr = &expr;
}
_ => return base_expr,
}
}
}
}

impl fmt::Debug for Expr {
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
allow_two_phase: AllowTwoPhase)
-> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
let expected = self.resolve_type_vars_with_obligations(expected);
let expr = expr.peel_drop_temps();
estebank marked this conversation as resolved.
Show resolved Hide resolved

let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase) {
Ok(ty) => return (ty, None),
Expand Down Expand Up @@ -355,6 +356,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let is_macro = sp.from_expansion() && !is_desugaring;

// `ExprKind::DropTemps` is semantically irrelevant for these suggestions.
let expr = expr.peel_drop_temps();

match (&expr.kind, &expected.kind, &checked_ty.kind) {
(_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.kind, &check.kind) {
(&ty::Str, &ty::Array(arr, _)) |
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

if let Some(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
let expr = expr.peel_drop_temps();
self.suggest_ref_or_into(&mut err, expr, expected_ty, ty);

let expr = match &expr.kind {
ExprKind::DropTemps(expr) => expr,
_ => expr,
};
extend_err(&mut err);
// Error possibly reported in `check_assign` so avoid emitting error again.
err.emit_unless(self.is_assign_to_bool(expr, expected_ty));
Expand Down
9 changes: 5 additions & 4 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4216,20 +4216,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn suggest_mismatched_types_on_tail(
&self,
err: &mut DiagnosticBuilder<'tcx>,
expression: &'tcx hir::Expr,
expr: &'tcx hir::Expr,
expected: Ty<'tcx>,
found: Ty<'tcx>,
cause_span: Span,
blk_id: hir::HirId,
) -> bool {
self.suggest_missing_semicolon(err, expression, expected, cause_span);
let expr = expr.peel_drop_temps();
self.suggest_missing_semicolon(err, expr, expected, cause_span);
let mut pointing_at_return_type = false;
if let Some((fn_decl, can_suggest)) = self.get_fn_decl(blk_id) {
pointing_at_return_type = self.suggest_missing_return_type(
err, &fn_decl, expected, found, can_suggest);
}
self.suggest_ref_or_into(err, expression, expected, found);
self.suggest_boxing_when_appropriate(err, expression, expected, found);
self.suggest_ref_or_into(err, expr, expected, found);
self.suggest_boxing_when_appropriate(err, expr, expected, found);
pointing_at_return_type
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/if/if-no-match-bindings.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LL | if &true {}
| ^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*&true`
| help: consider removing the borrow: `true`
|
= note: expected type `bool`
found type `&bool`
Expand All @@ -41,7 +41,7 @@ LL | if &mut true {}
| ^^^^^^^^^
| |
| expected bool, found &mut bool
| help: consider dereferencing the borrow: `*&mut true`
| help: consider removing the borrow: `true`
|
= note: expected type `bool`
found type `&mut bool`
Expand Down Expand Up @@ -77,7 +77,7 @@ LL | while &true {}
| ^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*&true`
| help: consider removing the borrow: `true`
|
= note: expected type `bool`
found type `&bool`
Expand All @@ -89,7 +89,7 @@ LL | while &mut true {}
| ^^^^^^^^^
| |
| expected bool, found &mut bool
| help: consider dereferencing the borrow: `*&mut true`
| help: consider removing the borrow: `true`
|
= note: expected type `bool`
found type `&mut bool`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ LL | if &let 0 = 0 {}
| ^^^^^^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*&let 0 = 0`
| help: consider removing the borrow: `let 0 = 0`
|
= note: expected type `bool`
found type `&bool`
Expand Down Expand Up @@ -708,7 +708,7 @@ LL | while &let 0 = 0 {}
| ^^^^^^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*&let 0 = 0`
| help: consider removing the borrow: `let 0 = 0`
|
= note: expected type `bool`
found type `&bool`
Expand Down