Skip to content

Wrap cast expressions inside of ValueTypeAscription #54938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,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()
Expand Down Expand Up @@ -719,7 +719,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: *user_ty,
}
} else {
cast
}
}
}
hir::ExprKind::Type(ref source, ref ty) => {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4140,6 +4140,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
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
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/nll/user-annotations/cast_static_lifetime.rs
Original file line number Diff line number Diff line change
@@ -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;
}