Skip to content

Commit 80ad300

Browse files
KiChjangnikomatsakis
authored andcommitted
Wrap cast expressions inside of ValueTypeAscription
1 parent e20fa70 commit 80ad300

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/librustc_mir/hair/cx/expr.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
637637
name: Field::new(cx.tcx.field_index(expr.id, cx.tables)),
638638
}
639639
}
640-
hir::ExprKind::Cast(ref source, _) => {
640+
hir::ExprKind::Cast(ref source, ref ty) => {
641641
// Check to see if this cast is a "coercion cast", where the cast is actually done
642642
// using a coercion (or is a no-op).
643643
if let Some(&TyCastKind::CoercionCast) = cx.tables()
@@ -714,7 +714,26 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
714714
} else {
715715
source.to_ref()
716716
};
717-
ExprKind::Cast { source }
717+
718+
let cast = ExprKind::Cast { source };
719+
720+
if let Some(user_ty) = cx.tables.user_provided_tys().get(ty.hir_id) {
721+
// NOTE: Creating a new Expr and wrapping a Cast inside of it may be
722+
// inefficient, revisit this when performance becomes an issue.
723+
let cast_expr = Expr {
724+
temp_lifetime,
725+
ty: expr_ty,
726+
span: expr.span,
727+
kind: cast,
728+
};
729+
730+
ExprKind::ValueTypeAscription {
731+
source: cast_expr.to_ref(),
732+
user_ty: UserTypeAnnotation::Ty(*user_ty),
733+
}
734+
} else {
735+
cast
736+
}
718737
}
719738
}
720739
hir::ExprKind::Type(ref source, ref ty) => {

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4166,6 +4166,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
41664166
let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
41674167
match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
41684168
Ok(cast_check) => {
4169+
let c_ty = self.infcx.canonicalize_response(&t_cast);
4170+
self.tables.borrow_mut().user_provided_tys_mut().insert(t.hir_id, c_ty);
41694171
deferred_cast_checks.push(cast_check);
41704172
t_cast
41714173
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(warnings)]
12+
#![feature(nll)]
13+
14+
fn main() {
15+
let x = 22_u32;
16+
let y: &u32 = (&x) as &'static u32;
17+
}

0 commit comments

Comments
 (0)