File tree 4 files changed +28
-3
lines changed
4 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,8 @@ enum CastError {
100
100
CastToBool ,
101
101
CastToChar ,
102
102
DifferingKinds ,
103
+ /// Cast of thin to fat raw ptr (eg. `*const () as *const [u8]`)
104
+ SizedUnsizedCast ,
103
105
IllegalCast ,
104
106
NeedViaPtr ,
105
107
NeedViaThinPtr ,
@@ -165,6 +167,13 @@ impl<'tcx> CastCheck<'tcx> {
165
167
fcx. infcx( ) . ty_to_string( self . cast_ty) )
166
168
} , self . expr_ty , None ) ;
167
169
}
170
+ CastError :: SizedUnsizedCast => {
171
+ fcx. type_error_message ( self . span , |actual| {
172
+ format ! ( "cannot cast thin pointer `{}` to fat pointer `{}`" ,
173
+ actual,
174
+ fcx. infcx( ) . ty_to_string( self . cast_ty) )
175
+ } , self . expr_ty , None )
176
+ }
168
177
CastError :: DifferingKinds => {
169
178
fcx. type_error_struct ( self . span , |actual| {
170
179
format ! ( "casting `{}` as `{}` is invalid" ,
@@ -312,7 +321,7 @@ impl<'tcx> CastCheck<'tcx> {
312
321
313
322
// sized -> unsized? report invalid cast (don't complain about vtable kinds)
314
323
if fcx. type_is_known_to_be_sized ( m_expr. ty , self . span ) {
315
- return Err ( CastError :: IllegalCast ) ;
324
+ return Err ( CastError :: SizedUnsizedCast ) ;
316
325
}
317
326
318
327
// vtable kinds must match
Original file line number Diff line number Diff line change @@ -87,7 +87,7 @@ fn main()
87
87
//~^^ HELP through a usize first
88
88
89
89
let _ = 42usize as * const [ u8 ] ; //~ ERROR casting
90
- let _ = v as * const [ u8 ] ; //~ ERROR casting
90
+ let _ = v as * const [ u8 ] ; //~ ERROR cannot cast
91
91
let _ = fat_v as * const Foo ;
92
92
//~^ ERROR `core::marker::Sized` is not implemented for the type `[u8]`
93
93
let _ = foo as * const str ; //~ ERROR casting
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ fn main() {
24
24
//~^^ HELP cast through a thin pointer
25
25
26
26
// #22955
27
- q as * const [ i32 ] ; //~ ERROR casting
27
+ q as * const [ i32 ] ; //~ ERROR cannot cast
28
28
29
29
// #21397
30
30
let t: * mut ( Trait + ' static ) = 0 as * mut _ ; //~ ERROR casting
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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 cast_thin_to_fat ( x : * const ( ) ) {
12
+ x as * const [ u8 ] ;
13
+ //~^ ERROR: cannot cast thin pointer `*const ()` to fat pointer `*const [u8]`
14
+ }
15
+
16
+ fn main ( ) { }
You can’t perform that action at this time.
0 commit comments