Skip to content

Commit 89bbd2c

Browse files
committed
Be a bit more constrained in our early check
Do not require the target type to be fully known, either. This allows code like `let x: *const () = 0 as _` to work (see regression test).
1 parent 2c9dfaf commit 89bbd2c

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/librustc_typeck/check/cast.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,18 @@ impl<'tcx> CastCheck<'tcx> {
128128
span: span,
129129
};
130130

131-
// For better error messages, we try to check whether the
132-
// target type is known to be sized now (we will also check
133-
// later, once inference is more complete done).
134-
if !fcx.type_is_known_to_be_sized(cast_ty, span) {
135-
check.report_cast_to_unsized_type(fcx);
136-
return Err(ErrorReported);
131+
// For better error messages, check for some obviously unsized
132+
// cases now. We do a more thorough check at the end, once
133+
// inference is more completely known.
134+
match cast_ty.sty {
135+
ty::TyTrait(..) | ty::TySlice(..) => {
136+
check.report_cast_to_unsized_type(fcx);
137+
Err(ErrorReported)
138+
}
139+
_ => {
140+
Ok(check)
141+
}
137142
}
138-
139-
Ok(check)
140143
}
141144

142145
fn report_cast_error<'a>(&self,

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3518,7 +3518,7 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
35183518
// Find the type of `e`. Supply hints based on the type we are casting to,
35193519
// if appropriate.
35203520
let t_cast = fcx.to_ty(t);
3521-
let t_cast = structurally_resolved_type(fcx, expr.span, t_cast);
3521+
let t_cast = fcx.infcx().resolve_type_vars_if_possible(&t_cast);
35223522
check_expr_with_expectation(fcx, e, ExpectCastableToType(t_cast));
35233523
let t_expr = fcx.expr_ty(e);
35243524
let t_cast = fcx.infcx().resolve_type_vars_if_possible(&t_cast);

src/test/run-pass/cast-to-infer-ty.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012 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+
// Check that we allow a cast to `_` so long as the target type can be
12+
// inferred elsewhere.
13+
14+
pub fn main() {
15+
let i: *const i32 = 0 as _;
16+
assert!(i.is_null());
17+
}

0 commit comments

Comments
 (0)