diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2016-08-17 15:59:27 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2016-08-17 19:12:37 +0200 |
| commit | feeed0b51bb4af89c300ee9c15bd3dfbf86778b1 (patch) | |
| tree | ceb8a51bfdca50f5406f676c23b56340411ca3d4 /src | |
| parent | d6d05904697d89099b55da3331155392f1db9c00 (diff) | |
| download | rust-feeed0b51bb4af89c300ee9c15bd3dfbf86778b1.tar.gz rust-feeed0b51bb4af89c300ee9c15bd3dfbf86778b1.zip | |
Fixes issue #11004
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_typeck/check/mod.rs | 4 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-11004.rs | 39 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index e573655b8c9..ff0b86aa595 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3000,6 +3000,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { but no field with that name was found", field.node, actual) }, expr_t); + if let ty::TyRawPtr(..) = expr_t.sty { + err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \ + `(*{0}).{1}`", pprust::expr_to_string(base), field.node)); + } if let ty::TyStruct(def, _) = expr_t.sty { Self::suggest_field_names(&mut err, def.struct_variant(), field, vec![]); } diff --git a/src/test/compile-fail/issue-11004.rs b/src/test/compile-fail/issue-11004.rs new file mode 100644 index 00000000000..308be462715 --- /dev/null +++ b/src/test/compile-fail/issue-11004.rs @@ -0,0 +1,39 @@ +// Copyright 2016 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. + +use std::mem; + +struct A { x: i32, y: f64 } + +#[cfg(not(works))] +unsafe fn access(n:*mut A) -> (i32, f64) { + let x : i32 = n.x; //~ ERROR attempted access of field `x` + //~| NOTE `n` is a native pointer; perhaps you need to deref with `(*n).x` + let y : f64 = n.y; //~ ERROR attempted access of field `y` + //~| NOTE `n` is a native pointer; perhaps you need to deref with `(*n).y` + (x, y) +} + +#[cfg(works)] +unsafe fn access(n:*mut A) -> (i32, f64) { + let x : i32 = (*n).x; + let y : f64 = (*n).y; + (x, y) +} + +fn main() { + let a : A = A { x: 3, y: 3.14 }; + let p : &A = &a; + let (x,y) = unsafe { + let n : *mut A = mem::transmute(p); + access(n) + }; + println!("x: {}, y: {}", x, y); +} |
