Skip to content

Commit 7e1b978

Browse files
committed
insert AscribeUserType for ascriptions
1 parent dd3cc96 commit 7e1b978

13 files changed

+126
-19
lines changed

src/librustc_mir/build/matches/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
329329
}
330330
}
331331

332+
self.ascribe_types(block, &candidate.ascriptions);
333+
332334
// now apply the bindings, which will also declare the variables
333335
self.bind_matched_candidate_for_arm_body(block, &candidate.bindings);
334336

@@ -958,6 +960,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
958960

959961
debug_assert!(candidate.match_pairs.is_empty());
960962

963+
self.ascribe_types(block, &candidate.ascriptions);
964+
961965
let arm_block = arm_blocks.blocks[candidate.arm_index];
962966
let candidate_source_info = self.source_info(candidate.span);
963967

@@ -1169,6 +1173,28 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
11691173
}
11701174
}
11711175

1176+
/// Append `AscribeUserType` statements onto the end of `block`
1177+
/// for each ascription
1178+
fn ascribe_types<'pat>(
1179+
&mut self,
1180+
block: BasicBlock,
1181+
ascriptions: &[Ascription<'tcx>],
1182+
) {
1183+
for ascription in ascriptions {
1184+
let source_info = self.source_info(ascription.span);
1185+
self.cfg.push(
1186+
block,
1187+
Statement {
1188+
source_info,
1189+
kind: StatementKind::AscribeUserType(
1190+
ascription.source.clone(),
1191+
ascription.user_ty,
1192+
),
1193+
},
1194+
);
1195+
}
1196+
}
1197+
11721198
// Only called when all_pat_vars_are_implicit_refs_within_guards,
11731199
// and thus all code/comments assume we are in that context.
11741200
fn bind_matched_candidate_for_guard(
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
error: unsatisfied lifetime constraints
2-
--> $DIR/associated-types-project-from-hrtb-in-fn-body.rs:32:29
2+
--> $DIR/associated-types-project-from-hrtb-in-fn-body.rs:32:19
33
|
44
LL | fn bar<'a, 'b, I : for<'x> Foo<&'x isize>>(
55
| -- -- lifetime `'b` defined here
66
| |
77
| lifetime `'a` defined here
88
...
99
LL | let z: I::A = if cond { x } else { y };
10-
| ^ assignment requires that `'a` must outlive `'b`
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'b`
1111

1212
error: unsatisfied lifetime constraints
13-
--> $DIR/associated-types-project-from-hrtb-in-fn-body.rs:32:40
13+
--> $DIR/associated-types-project-from-hrtb-in-fn-body.rs:32:19
1414
|
1515
LL | fn bar<'a, 'b, I : for<'x> Foo<&'x isize>>(
1616
| -- -- lifetime `'b` defined here
1717
| |
1818
| lifetime `'a` defined here
1919
...
2020
LL | let z: I::A = if cond { x } else { y };
21-
| ^ assignment requires that `'b` must outlive `'a`
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'b` must outlive `'a`
2222

2323
error: aborting due to 2 previous errors
2424

src/test/ui/borrowck/borrowck-access-permissions.ast.nll.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ error[E0596]: cannot borrow `*ptr_x` as mutable, as it is behind a `*const` poin
3535
--> $DIR/borrowck-access-permissions.rs:56:23
3636
|
3737
LL | let ptr_x : *const _ = &x;
38-
| -- help: consider changing this to be a mutable pointer: `&mut x`
38+
| ----- help: consider changing this to be a mutable pointer: `*mut i32`
3939
...
4040
LL | let _y1 = &mut *ptr_x; //[ast]~ ERROR [E0596]
4141
| ^^^^^^^^^^^ `ptr_x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable

src/test/ui/borrowck/borrowck-access-permissions.mir.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ error[E0596]: cannot borrow `*ptr_x` as mutable, as it is behind a `*const` poin
3535
--> $DIR/borrowck-access-permissions.rs:56:23
3636
|
3737
LL | let ptr_x : *const _ = &x;
38-
| -- help: consider changing this to be a mutable pointer: `&mut x`
38+
| ----- help: consider changing this to be a mutable pointer: `*mut i32`
3939
...
4040
LL | let _y1 = &mut *ptr_x; //[ast]~ ERROR [E0596]
4141
| ^^^^^^^^^^^ `ptr_x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable

src/test/ui/consts/const-eval/dont_promote_unstable_const_fn.nll.stderr

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ LL | const fn bar() -> u32 { foo() } //~ ERROR `foo` is not yet stable as a cons
66
|
77
= help: in Nightly builds, add `#![feature(foo)]` to the crate attributes to enable
88

9+
error[E0597]: borrowed value does not live long enough
10+
--> $DIR/dont_promote_unstable_const_fn.rs:28:28
11+
|
12+
LL | let _: &'static u32 = &foo(); //~ ERROR does not live long enough
13+
| ^^^^^ temporary value does not live long enough
14+
LL | }
15+
| - temporary value only lives until here
16+
|
17+
= note: borrowed value must be valid for the static lifetime...
18+
19+
error[E0597]: borrowed value does not live long enough
20+
--> $DIR/dont_promote_unstable_const_fn.rs:32:28
21+
|
22+
LL | let _: &'static u32 = &meh(); //~ ERROR does not live long enough
23+
| ^^^^^ temporary value does not live long enough
24+
LL | let x: &'static _ = &std::time::Duration::from_millis(42).subsec_millis();
25+
LL | }
26+
| - temporary value only lives until here
27+
|
28+
= note: borrowed value must be valid for the static lifetime...
29+
930
error[E0597]: borrowed value does not live long enough
1031
--> $DIR/dont_promote_unstable_const_fn.rs:33:26
1132
|
@@ -17,6 +38,6 @@ LL | }
1738
|
1839
= note: borrowed value must be valid for the static lifetime...
1940

20-
error: aborting due to 2 previous errors
41+
error: aborting due to 4 previous errors
2142

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

src/test/ui/nll/relate_tys/hr-fn-aaa-as-aba.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ fn make_it() -> for<'a> fn(&'a u32, &'a u32) -> &'a u32 {
2323
fn main() {
2424
let a: for<'a, 'b> fn(&'a u32, &'b u32) -> &'a u32 = make_it();
2525
//~^ ERROR higher-ranked subtype error
26+
//~| ERROR higher-ranked subtype error
2627
drop(a);
2728
}

src/test/ui/nll/relate_tys/hr-fn-aaa-as-aba.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@ error: higher-ranked subtype error
44
LL | let a: for<'a, 'b> fn(&'a u32, &'b u32) -> &'a u32 = make_it();
55
| ^^^^^^^^^
66

7-
error: aborting due to previous error
7+
error: higher-ranked subtype error
8+
--> $DIR/hr-fn-aaa-as-aba.rs:24:9
9+
|
10+
LL | let a: for<'a, 'b> fn(&'a u32, &'b u32) -> &'a u32 = make_it();
11+
| ^
12+
13+
error: aborting due to 2 previous errors
814

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Test that various patterns also enforce types.
2+
3+
#![feature(nll)]
4+
5+
fn main() {
6+
let _: Vec<&'static String> = vec![&String::new()];
7+
//~^ ERROR borrowed value does not live long enough [E0597]
8+
9+
let (_, a): (Vec<&'static String>, _) = (vec![&String::new()], 44);
10+
//~^ ERROR borrowed value does not live long enough [E0597]
11+
12+
let (_a, b): (Vec<&'static String>, _) = (vec![&String::new()], 44);
13+
//~^ ERROR borrowed value does not live long enough [E0597]
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0597]: borrowed value does not live long enough
2+
--> $DIR/patterns.rs:6:41
3+
|
4+
LL | let _: Vec<&'static String> = vec![&String::new()];
5+
| ^^^^^^^^^^^^^ - temporary value only lives until here
6+
| |
7+
| temporary value does not live long enough
8+
|
9+
= note: borrowed value must be valid for the static lifetime...
10+
11+
error[E0597]: borrowed value does not live long enough
12+
--> $DIR/patterns.rs:9:52
13+
|
14+
LL | let (_, a): (Vec<&'static String>, _) = (vec![&String::new()], 44);
15+
| ^^^^^^^^^^^^^ - temporary value only lives until here
16+
| |
17+
| temporary value does not live long enough
18+
|
19+
= note: borrowed value must be valid for the static lifetime...
20+
21+
error[E0597]: borrowed value does not live long enough
22+
--> $DIR/patterns.rs:12:53
23+
|
24+
LL | let (_a, b): (Vec<&'static String>, _) = (vec![&String::new()], 44);
25+
| ^^^^^^^^^^^^^ - temporary value only lives until here
26+
| |
27+
| temporary value does not live long enough
28+
|
29+
= note: borrowed value must be valid for the static lifetime...
30+
31+
error: aborting due to 3 previous errors
32+
33+
For more information about this error, try `rustc --explain E0597`.

src/test/ui/regions/region-object-lifetime-in-coercion.nll.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
error[E0621]: explicit lifetime required in the type of `v`
2-
--> $DIR/region-object-lifetime-in-coercion.rs:18:33
2+
--> $DIR/region-object-lifetime-in-coercion.rs:20:5
33
|
44
LL | fn a(v: &[u8]) -> Box<Foo + 'static> {
55
| ----- help: add explicit lifetime `'static` to the type of `v`: `&'static [u8]`
6-
LL | let x: Box<Foo + 'static> = Box::new(v);
7-
| ^^^^^^^^^^^ lifetime `'static` required
6+
...
7+
LL | x
8+
| ^ lifetime `'static` required
89

910
error[E0621]: explicit lifetime required in the type of `v`
1011
--> $DIR/region-object-lifetime-in-coercion.rs:24:5

src/test/ui/slice-mut-2.nll.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
22
--> $DIR/slice-mut-2.rs:17:18
33
|
44
LL | let x: &[isize] = &[1, 2, 3, 4, 5];
5-
| ---------------- help: consider changing this to be a mutable reference: `&mut [1, 2, 3, 4, 5]`
5+
| - help: consider changing this to be a mutable reference: `&mut [isize]`
66
...
77
LL | let _ = &mut x[2..4]; //~ERROR cannot borrow immutable borrowed content `*x` as mutable
88
| ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable

src/test/ui/try-block/try-block-bad-lifetime.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
error[E0597]: `my_string` does not live long enough
22
--> $DIR/try-block-bad-lifetime.rs:25:33
33
|
4+
LL | let result: Result<(), &str> = try {
5+
| ------ borrow later used here
6+
LL | let my_string = String::from("");
47
LL | let my_str: & str = & my_string;
58
| ^^^^^^^^^^^ borrowed value does not live long enough
69
...
710
LL | };
811
| - `my_string` dropped here while still borrowed
9-
LL | do_something_with(result);
10-
| ------ borrow later used here
1112

1213
error[E0506]: cannot assign to `i` because it is borrowed
1314
--> $DIR/try-block-bad-lifetime.rs:39:13

src/test/ui/unboxed-closures/unboxed-closures-failed-recursive-fn-1.nll.stderr

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ LL | let g = factorial.as_ref().unwrap();
3838
| ^^^^^^^^^ borrowed value does not live long enough
3939
...
4040
LL | }
41-
| - `factorial` dropped here while still borrowed
41+
| -
42+
| |
43+
| `factorial` dropped here while still borrowed
44+
| borrow later used here, when `factorial` is dropped
4245
|
43-
= note: borrowed value must be valid for the static lifetime...
46+
= note: values in a scope are dropped in the opposite order they are defined
4447

4548
error[E0506]: cannot assign to `factorial` because it is borrowed
4649
--> $DIR/unboxed-closures-failed-recursive-fn-1.rs:42:5
@@ -52,9 +55,10 @@ LL | let g = factorial.as_ref().unwrap();
5255
| --------- borrow occurs due to use in closure
5356
...
5457
LL | factorial = Some(Box::new(f));
55-
| ^^^^^^^^^ assignment to borrowed `factorial` occurs here
56-
|
57-
= note: borrowed value must be valid for the static lifetime...
58+
| ^^^^^^^^^
59+
| |
60+
| assignment to borrowed `factorial` occurs here
61+
| borrow later used here
5862

5963
error: aborting due to 4 previous errors
6064

0 commit comments

Comments
 (0)