Skip to content

Commit 802c897

Browse files
committed
review comments: (marginally) reduce memory consumtion
1 parent 7eb6a2a commit 802c897

File tree

6 files changed

+68
-12
lines changed

6 files changed

+68
-12
lines changed

Diff for: src/librustc/infer/error_reporting/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
512512
ObligationCauseCode::MatchExpressionArm {
513513
source,
514514
ref prior_arms,
515+
last_ty,
515516
..
516517
} => match source {
517518
hir::MatchSource::IfLetDesugar { .. } => {
@@ -522,13 +523,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
522523
_ => {
523524
let msg = "`match` arms have incompatible types";
524525
err.span_label(cause.span, msg);
525-
if prior_arms.len() < 4 {
526-
for (sp, ty) in prior_arms {
527-
err.span_label(*sp, format!("this is found to be of type `{}`", ty));
526+
if prior_arms.len() <= 4 {
527+
for sp in prior_arms {
528+
err.span_label(*sp, format!(
529+
"this is found to be of type `{}`",
530+
last_ty,
531+
));
528532
}
529-
} else if let Some((sp, ty)) = prior_arms.last() {
533+
} else if let Some(sp) = prior_arms.last() {
530534
err.span_label(*sp, format!(
531-
"this and all prior arms are found to be of type `{}`", ty,
535+
"this and all prior arms are found to be of type `{}`", last_ty,
532536
));
533537
}
534538
}

Diff for: src/librustc/traits/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ pub enum ObligationCauseCode<'tcx> {
224224
MatchExpressionArm {
225225
arm_span: Span,
226226
source: hir::MatchSource,
227-
prior_arms: Vec<(Span, Ty<'tcx>)>,
227+
prior_arms: Vec<Span>,
228+
last_ty: Ty<'tcx>,
228229
},
229230

230231
/// Computing common supertype in the pattern guard for the arms of a match expression

Diff for: src/librustc/traits/structural_impls.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,16 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
517517
arm_span,
518518
source,
519519
ref prior_arms,
520+
last_ty,
520521
} => {
521-
let prior_arms = prior_arms.iter().filter_map(|(sp, ty)| {
522-
tcx.lift(ty).map(|ty| (*sp, ty))
523-
}).collect();
524-
Some(super::MatchExpressionArm { arm_span, source, prior_arms })
522+
tcx.lift(&last_ty).map(|last_ty| {
523+
super::MatchExpressionArm {
524+
arm_span,
525+
source,
526+
prior_arms: prior_arms.clone(),
527+
last_ty,
528+
}
529+
})
525530
}
526531
super::MatchExpressionArmPattern { span, ty } => {
527532
tcx.lift(&ty).map(|ty| super::MatchExpressionArmPattern { span, ty })

Diff for: src/librustc_typeck/check/_match.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
690690
};
691691

692692
let mut other_arms = vec![]; // used only for diagnostics
693+
let mut prior_arm_ty = None;
693694
for (i, (arm, pats_diverge)) in arms.iter().zip(all_arm_pats_diverge).enumerate() {
694695
if let Some(ref g) = arm.guard {
695696
self.diverges.set(pats_diverge);
@@ -730,11 +731,16 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
730731
arm_span,
731732
source: match_src,
732733
prior_arms: other_arms.clone(),
734+
last_ty: prior_arm_ty.unwrap(),
733735
})
734736
};
735737
coercion.coerce(self, &cause, &arm.body, arm_ty);
736738
}
737-
other_arms.push((arm_span, arm_ty));
739+
other_arms.push(arm_span);
740+
if other_arms.len() > 5 {
741+
other_arms.remove(0);
742+
}
743+
prior_arm_ty = Some(arm_ty);
738744
}
739745

740746
// We won't diverge unless the discriminant or all arms diverge.

Diff for: src/test/ui/match/match-type-err-first-arm.rs

+18
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,21 @@ fn test_func2(n: i32) -> i32 {
2525
};
2626
x
2727
}
28+
29+
fn test_func3(n: i32) -> i32 {
30+
let x = match n {
31+
//~^ NOTE `match` arms have incompatible types
32+
1 => 'b',
33+
2 => 'b',
34+
3 => 'b',
35+
4 => 'b',
36+
5 => 'b',
37+
6 => 'b',
38+
//~^ NOTE this and all prior arms are found to be of type `char`
39+
_ => 42,
40+
//~^ ERROR match arms have incompatible types
41+
//~| NOTE expected char, found integer
42+
//~| NOTE expected type `char`
43+
};
44+
x
45+
}

Diff for: src/test/ui/match/match-type-err-first-arm.stderr

+23-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ LL | | };
2626
= note: expected type `char`
2727
found type `{integer}`
2828

29-
error: aborting due to 2 previous errors
29+
error[E0308]: match arms have incompatible types
30+
--> $DIR/match-type-err-first-arm.rs:39:14
31+
|
32+
LL | let x = match n {
33+
| _____________-
34+
LL | | //~^ NOTE `match` arms have incompatible types
35+
LL | | 1 => 'b',
36+
LL | | 2 => 'b',
37+
... |
38+
LL | | 6 => 'b',
39+
| | --- this and all prior arms are found to be of type `char`
40+
LL | | //~^ NOTE this and all prior arms are found to be of type `char`
41+
LL | | _ => 42,
42+
| | ^^ expected char, found integer
43+
... |
44+
LL | | //~| NOTE expected type `char`
45+
LL | | };
46+
| |_____- `match` arms have incompatible types
47+
|
48+
= note: expected type `char`
49+
found type `{integer}`
50+
51+
error: aborting due to 3 previous errors
3052

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

0 commit comments

Comments
 (0)