Skip to content

Commit a66ab2b

Browse files
committed
skip user-type annotations if they don't have regions
1 parent d5d5e8c commit a66ab2b

File tree

7 files changed

+43
-55
lines changed

7 files changed

+43
-55
lines changed

src/librustc_mir/build/expr/as_place.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,39 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
139139

140140
ExprKind::PlaceTypeAscription { source, user_ty } => {
141141
let place = unpack!(block = this.as_place(block, source));
142-
this.cfg.push(
143-
block,
144-
Statement {
145-
source_info,
146-
kind: StatementKind::AscribeUserType(
147-
place.clone(),
148-
Variance::Invariant,
149-
user_ty,
150-
),
151-
},
152-
);
142+
if let Some(user_ty) = user_ty {
143+
this.cfg.push(
144+
block,
145+
Statement {
146+
source_info,
147+
kind: StatementKind::AscribeUserType(
148+
place.clone(),
149+
Variance::Invariant,
150+
user_ty,
151+
),
152+
},
153+
);
154+
}
153155
block.and(place)
154156
}
155157
ExprKind::ValueTypeAscription { source, user_ty } => {
156158
let source = this.hir.mirror(source);
157159
let temp = unpack!(
158160
block = this.as_temp(block, source.temp_lifetime, source, mutability)
159161
);
160-
this.cfg.push(
161-
block,
162-
Statement {
163-
source_info,
164-
kind: StatementKind::AscribeUserType(
165-
Place::Local(temp.clone()),
166-
Variance::Invariant,
167-
user_ty,
168-
),
169-
},
170-
);
162+
if let Some(user_ty) = user_ty {
163+
this.cfg.push(
164+
block,
165+
Statement {
166+
source_info,
167+
kind: StatementKind::AscribeUserType(
168+
Place::Local(temp.clone()),
169+
Variance::Invariant,
170+
user_ty,
171+
),
172+
},
173+
);
174+
}
171175
block.and(Place::Local(temp))
172176
}
173177

src/librustc_mir/hair/cx/expr.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -749,23 +749,15 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
749749

750750
ExprKind::ValueTypeAscription {
751751
source: cast_expr.to_ref(),
752-
user_ty: user_ty,
752+
user_ty: Some(user_ty),
753753
}
754754
} else {
755755
cast
756756
}
757757
}
758758
hir::ExprKind::Type(ref source, ref ty) => {
759759
let user_provided_tys = cx.tables.user_provided_tys();
760-
let user_ty = UserTypeAnnotation::Ty(
761-
*user_provided_tys
762-
.get(ty.hir_id)
763-
.expect(&format!(
764-
"{:?} not found in user_provided_tys, source: {:?}",
765-
ty,
766-
source,
767-
))
768-
);
760+
let user_ty = user_provided_tys.get(ty.hir_id).map(|&c_ty| UserTypeAnnotation::Ty(c_ty));
769761
if source.is_place_expr() {
770762
ExprKind::PlaceTypeAscription {
771763
source: source.to_ref(),

src/librustc_mir/hair/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,12 @@ pub enum ExprKind<'tcx> {
276276
PlaceTypeAscription {
277277
source: ExprRef<'tcx>,
278278
/// Type that the user gave to this expression
279-
user_ty: UserTypeAnnotation<'tcx>,
279+
user_ty: Option<UserTypeAnnotation<'tcx>>,
280280
},
281281
ValueTypeAscription {
282282
source: ExprRef<'tcx>,
283283
/// Type that the user gave to this expression
284-
user_ty: UserTypeAnnotation<'tcx>,
284+
user_ty: Option<UserTypeAnnotation<'tcx>>,
285285
},
286286
Closure {
287287
closure_id: DefId,

src/librustc_typeck/check/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,8 +2359,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
23592359

23602360
pub fn to_ty_saving_user_provided_ty(&self, ast_ty: &hir::Ty) -> Ty<'tcx> {
23612361
let ty = self.to_ty(ast_ty);
2362-
let c_ty = self.infcx.canonicalize_response(&ty);
2363-
self.tables.borrow_mut().user_provided_tys_mut().insert(ast_ty.hir_id, c_ty);
2362+
2363+
// If the type given by the user has free regions, save it for
2364+
// later, since NLL would like to enforce those. Other sorts
2365+
// of things are already sufficiently enforced. =)
2366+
if ty.has_free_regions() {
2367+
let c_ty = self.infcx.canonicalize_response(&ty);
2368+
self.tables.borrow_mut().user_provided_tys_mut().insert(ast_ty.hir_id, c_ty);
2369+
}
2370+
23642371
ty
23652372
}
23662373

src/test/ui/consts/const-eval/conditional_array_execution.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const X: u32 = 5;
1414
const Y: u32 = 6;
1515
const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
1616
//~^ WARN this constant cannot be used
17-
//~| ERROR
1817

1918
fn main() {
2019
println!("{}", FOO);

src/test/ui/consts/const-eval/conditional_array_execution.stderr

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,20 @@ LL | #![warn(const_err)]
1313
| ^^^^^^^^^
1414

1515
error[E0080]: referenced constant has errors
16-
--> $DIR/conditional_array_execution.rs:20:20
16+
--> $DIR/conditional_array_execution.rs:19:20
1717
|
1818
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
1919
| ----- attempt to subtract with overflow
2020
...
2121
LL | println!("{}", FOO);
2222
| ^^^
2323

24-
error[E0080]: could not evaluate constant
25-
--> $DIR/conditional_array_execution.rs:20:20
24+
error[E0080]: erroneous constant used
25+
--> $DIR/conditional_array_execution.rs:19:20
2626
|
2727
LL | println!("{}", FOO);
2828
| ^^^ referenced constant has errors
2929

30-
error[E0080]: constant evaluation error
31-
--> $DIR/conditional_array_execution.rs:15:1
32-
|
33-
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
34-
| ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
35-
| |
36-
| attempt to subtract with overflow
37-
38-
error: aborting due to 3 previous errors
30+
error: aborting due to 2 previous errors
3931

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

src/test/ui/consts/const-eval/promoted_errors.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ warning: attempt to divide by zero
3434
LL | println!("{}", 1/(false as u32));
3535
| ^^^^^^^^^^^^^^^^
3636

37-
warning: this expression will panic at runtime
38-
--> $DIR/promoted_errors.rs:24:20
39-
|
40-
LL | println!("{}", 1/(false as u32));
41-
| ^^^^^^^^^^^^^^^^ attempt to divide by zero
42-
4337
warning: attempt to divide by zero
4438
--> $DIR/promoted_errors.rs:26:14
4539
|

0 commit comments

Comments
 (0)