Skip to content

Commit

Permalink
review comments: (marginally) reduce memory consumtion
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Feb 8, 2019
1 parent 7eb6a2a commit 802c897
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
ObligationCauseCode::MatchExpressionArm {
source,
ref prior_arms,
last_ty,
..
} => match source {
hir::MatchSource::IfLetDesugar { .. } => {
Expand All @@ -522,13 +523,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
_ => {
let msg = "`match` arms have incompatible types";
err.span_label(cause.span, msg);
if prior_arms.len() < 4 {
for (sp, ty) in prior_arms {
err.span_label(*sp, format!("this is found to be of type `{}`", ty));
if prior_arms.len() <= 4 {
for sp in prior_arms {
err.span_label(*sp, format!(
"this is found to be of type `{}`",
last_ty,
));
}
} else if let Some((sp, ty)) = prior_arms.last() {
} else if let Some(sp) = prior_arms.last() {
err.span_label(*sp, format!(
"this and all prior arms are found to be of type `{}`", ty,
"this and all prior arms are found to be of type `{}`", last_ty,
));
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ pub enum ObligationCauseCode<'tcx> {
MatchExpressionArm {
arm_span: Span,
source: hir::MatchSource,
prior_arms: Vec<(Span, Ty<'tcx>)>,
prior_arms: Vec<Span>,
last_ty: Ty<'tcx>,
},

/// Computing common supertype in the pattern guard for the arms of a match expression
Expand Down
13 changes: 9 additions & 4 deletions src/librustc/traits/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,16 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
arm_span,
source,
ref prior_arms,
last_ty,
} => {
let prior_arms = prior_arms.iter().filter_map(|(sp, ty)| {
tcx.lift(ty).map(|ty| (*sp, ty))
}).collect();
Some(super::MatchExpressionArm { arm_span, source, prior_arms })
tcx.lift(&last_ty).map(|last_ty| {
super::MatchExpressionArm {
arm_span,
source,
prior_arms: prior_arms.clone(),
last_ty,
}
})
}
super::MatchExpressionArmPattern { span, ty } => {
tcx.lift(&ty).map(|ty| super::MatchExpressionArmPattern { span, ty })
Expand Down
8 changes: 7 additions & 1 deletion src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
};

let mut other_arms = vec![]; // used only for diagnostics
let mut prior_arm_ty = None;
for (i, (arm, pats_diverge)) in arms.iter().zip(all_arm_pats_diverge).enumerate() {
if let Some(ref g) = arm.guard {
self.diverges.set(pats_diverge);
Expand Down Expand Up @@ -730,11 +731,16 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
arm_span,
source: match_src,
prior_arms: other_arms.clone(),
last_ty: prior_arm_ty.unwrap(),
})
};
coercion.coerce(self, &cause, &arm.body, arm_ty);
}
other_arms.push((arm_span, arm_ty));
other_arms.push(arm_span);
if other_arms.len() > 5 {
other_arms.remove(0);
}
prior_arm_ty = Some(arm_ty);
}

// We won't diverge unless the discriminant or all arms diverge.
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/match/match-type-err-first-arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ fn test_func2(n: i32) -> i32 {
};
x
}

fn test_func3(n: i32) -> i32 {
let x = match n {
//~^ NOTE `match` arms have incompatible types
1 => 'b',
2 => 'b',
3 => 'b',
4 => 'b',
5 => 'b',
6 => 'b',
//~^ NOTE this and all prior arms are found to be of type `char`
_ => 42,
//~^ ERROR match arms have incompatible types
//~| NOTE expected char, found integer
//~| NOTE expected type `char`
};
x
}
24 changes: 23 additions & 1 deletion src/test/ui/match/match-type-err-first-arm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ LL | | };
= note: expected type `char`
found type `{integer}`

error: aborting due to 2 previous errors
error[E0308]: match arms have incompatible types
--> $DIR/match-type-err-first-arm.rs:39:14
|
LL | let x = match n {
| _____________-
LL | | //~^ NOTE `match` arms have incompatible types
LL | | 1 => 'b',
LL | | 2 => 'b',
... |
LL | | 6 => 'b',
| | --- this and all prior arms are found to be of type `char`
LL | | //~^ NOTE this and all prior arms are found to be of type `char`
LL | | _ => 42,
| | ^^ expected char, found integer
... |
LL | | //~| NOTE expected type `char`
LL | | };
| |_____- `match` arms have incompatible types
|
= note: expected type `char`
found type `{integer}`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 802c897

Please sign in to comment.