Wrap cast expressions inside of ValueTypeAscription

This commit is contained in:
Keith Yeung 2018-10-02 09:31:46 -07:00 committed by Niko Matsakis
parent e20fa70bb3
commit 80ad300b89
3 changed files with 40 additions and 2 deletions

View File

@ -637,7 +637,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
name: Field::new(cx.tcx.field_index(expr.id, cx.tables)),
}
}
hir::ExprKind::Cast(ref source, _) => {
hir::ExprKind::Cast(ref source, ref ty) => {
// Check to see if this cast is a "coercion cast", where the cast is actually done
// using a coercion (or is a no-op).
if let Some(&TyCastKind::CoercionCast) = cx.tables()
@ -714,7 +714,26 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
} else {
source.to_ref()
};
ExprKind::Cast { source }
let cast = ExprKind::Cast { source };
if let Some(user_ty) = cx.tables.user_provided_tys().get(ty.hir_id) {
// NOTE: Creating a new Expr and wrapping a Cast inside of it may be
// inefficient, revisit this when performance becomes an issue.
let cast_expr = Expr {
temp_lifetime,
ty: expr_ty,
span: expr.span,
kind: cast,
};
ExprKind::ValueTypeAscription {
source: cast_expr.to_ref(),
user_ty: UserTypeAnnotation::Ty(*user_ty),
}
} else {
cast
}
}
}
hir::ExprKind::Type(ref source, ref ty) => {

View File

@ -4166,6 +4166,8 @@ fn check_expr_kind(
let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
Ok(cast_check) => {
let c_ty = self.infcx.canonicalize_response(&t_cast);
self.tables.borrow_mut().user_provided_tys_mut().insert(t.hir_id, c_ty);
deferred_cast_checks.push(cast_check);
t_cast
}

View File

@ -0,0 +1,17 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(warnings)]
#![feature(nll)]
fn main() {
let x = 22_u32;
let y: &u32 = (&x) as &'static u32;
}