Skip to content

Commit 9f6e5ae

Browse files
committed
Rollup merge of rust-lang#48270 - leodasvacas:refactor-casts, r=nikomatsakis
Replace `structurally_resolved_type` in casts check. The behaviour of `resolve_type_vars_if_possible` is simpler and infallible. Other minor refactorings. I'm not sure if this is backwards compatible, in theory resolving obligations between two cast checks could solve a dependency between them, but I don't know if that's actually possible and it doesn't sound like something we'd want to support.
2 parents 484e6f0 + 0a5a5c3 commit 9f6e5ae

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

src/librustc/ty/cast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use syntax::ast;
2020
pub enum IntTy {
2121
U(ast::UintTy),
2222
I,
23-
Ivar,
2423
CEnum,
2524
Bool,
2625
Char
@@ -64,7 +63,7 @@ impl<'tcx> CastTy<'tcx> {
6463
ty::TyBool => Some(CastTy::Int(IntTy::Bool)),
6564
ty::TyChar => Some(CastTy::Int(IntTy::Char)),
6665
ty::TyInt(_) => Some(CastTy::Int(IntTy::I)),
67-
ty::TyInfer(ty::InferTy::IntVar(_)) => Some(CastTy::Int(IntTy::Ivar)),
66+
ty::TyInfer(ty::InferTy::IntVar(_)) => Some(CastTy::Int(IntTy::I)),
6867
ty::TyInfer(ty::InferTy::FloatVar(_)) => Some(CastTy::Float),
6968
ty::TyUint(u) => Some(CastTy::Int(IntTy::U(u))),
7069
ty::TyFloat(_) => Some(CastTy::Float),

src/librustc_typeck/check/cast.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
486486
ty::TypeVariants::TyInfer(t) => {
487487
match t {
488488
ty::InferTy::IntVar(_) |
489-
ty::InferTy::FloatVar(_) |
490-
ty::InferTy::FreshIntTy(_) |
491-
ty::InferTy::FreshFloatTy(_) => {
492-
Err(CastError::NeedDeref)
493-
}
489+
ty::InferTy::FloatVar(_) => Err(CastError::NeedDeref),
494490
_ => Err(CastError::NeedViaPtr),
495491
}
496492
}

src/test/run-pass/cast.rs

+5
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ pub fn main() {
1919
assert_eq!(i as u8 as i8, 'Q' as u8 as i8);
2020
assert_eq!(0x51 as char, 'Q');
2121
assert_eq!(0 as u32, false as u32);
22+
23+
// Test that `_` is correctly inferred.
24+
let x = &"hello";
25+
let mut y = x as *const _;
26+
y = 0 as *const _;
2227
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 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+
fn main() {
12+
// Tests case where inference fails due to the order in which casts are checked.
13+
// Ideally this would compile, see #48270.
14+
let x = &"hello";
15+
let mut y = 0 as *const _;
16+
//~^ ERROR cannot cast to a pointer of an unknown kind
17+
y = x as *const _;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0641]: cannot cast to a pointer of an unknown kind
2+
--> $DIR/order-dependent-cast-inference.rs:15:17
3+
|
4+
LL | let mut y = 0 as *const _;
5+
| ^^^^^--------
6+
| |
7+
| help: consider giving more type information
8+
|
9+
= note: The type information given here is insufficient to check whether the pointer cast is valid
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0641`.

0 commit comments

Comments
 (0)