Skip to content

Commit 9b72650

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#36723 - GuillaumeGomez:e0513, r=jonathandturner
E0513 Part of rust-lang#35233 r? @jonathandturner
2 parents 02cc578 + 96a0f06 commit 9b72650

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

src/librustc_typeck/check/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1525,9 +1525,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
15251525
match self.locals.borrow().get(&nid) {
15261526
Some(&t) => t,
15271527
None => {
1528-
span_err!(self.tcx.sess, span, E0513,
1529-
"no type for local variable {}",
1530-
nid);
1528+
struct_span_err!(self.tcx.sess, span, E0513,
1529+
"no type for local variable {}",
1530+
self.tcx.map.node_to_string(nid))
1531+
.span_label(span, &"no type for variable")
1532+
.emit();
15311533
self.tcx.types.err
15321534
}
15331535
}

src/librustc_typeck/diagnostics.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -3766,6 +3766,45 @@ extern "platform-intrinsic" {
37663766
```
37673767
"##,
37683768

3769+
E0513: r##"
3770+
The type of the variable couldn't be found out.
3771+
3772+
Erroneous code example:
3773+
3774+
```compile_fail,E0513
3775+
use std::mem;
3776+
3777+
unsafe {
3778+
let size = mem::size_of::<u32>();
3779+
mem::transmute_copy::<u32, [u8; size]>(&8_8);
3780+
// error: no type for local variable
3781+
}
3782+
```
3783+
3784+
To fix this error, please use a constant size instead of `size`. To make
3785+
this error more obvious, you could run:
3786+
3787+
```compile_fail,E0080
3788+
use std::mem;
3789+
3790+
unsafe {
3791+
mem::transmute_copy::<u32, [u8; mem::size_of::<u32>()]>(&8_8);
3792+
// error: constant evaluation error
3793+
}
3794+
```
3795+
3796+
So now, you can fix your code by setting the size directly:
3797+
3798+
```
3799+
use std::mem;
3800+
3801+
unsafe {
3802+
mem::transmute_copy::<u32, [u8; 4]>(&8_8);
3803+
// `u32` is 4 bytes so we replace the `mem::size_of` call with its size
3804+
}
3805+
```
3806+
"##,
3807+
37693808
E0516: r##"
37703809
The `typeof` keyword is currently reserved but unimplemented.
37713810
Erroneous code example:
@@ -4064,7 +4103,6 @@ register_diagnostics! {
40644103
E0399, // trait items need to be implemented because the associated
40654104
// type `{}` was overridden
40664105
E0436, // functional record update requires a struct
4067-
E0513, // no type for local variable ..
40684106
E0521, // redundant default implementations of trait
40694107
E0533, // `{}` does not name a unit variant, unit struct or a constant
40704108
E0562, // `impl Trait` not allowed outside of function

src/test/compile-fail/E0513.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
use std::mem;
12+
13+
fn main() {
14+
unsafe {
15+
let size = mem::size_of::<u32>();
16+
mem::transmute_copy::<u32, [u8; size]>(&8_8); //~ ERROR E0513
17+
//~| NOTE no type for variable
18+
}
19+
}

0 commit comments

Comments
 (0)