diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index 9d1bcb8164a9e..627753039bae3 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -202,7 +202,9 @@ impl<'tcx> cmt_<'tcx> { Categorization::Downcast(ref cmt, _) => { if let Categorization::Local(_) = cmt.cat { if let ty::TyAdt(def, _) = self.ty.sty { - return def.struct_variant().find_field_named(name).map(|x| x.did); + if def.is_struct() { + return def.struct_variant().find_field_named(name).map(|x| x.did); + } } None } else { diff --git a/src/test/ui/did_you_mean/issue-39544.rs b/src/test/ui/did_you_mean/issue-39544.rs new file mode 100644 index 0000000000000..bcdafefa2472b --- /dev/null +++ b/src/test/ui/did_you_mean/issue-39544.rs @@ -0,0 +1,22 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +enum X { + Y +} + +struct Z { + x: X +} + +fn main() { + let z = Z { x: X::Y }; + let _ = &mut z.x; +} diff --git a/src/test/ui/did_you_mean/issue-39544.stderr b/src/test/ui/did_you_mean/issue-39544.stderr new file mode 100644 index 0000000000000..c0088f39ad3e1 --- /dev/null +++ b/src/test/ui/did_you_mean/issue-39544.stderr @@ -0,0 +1,8 @@ +error: cannot borrow immutable field `z.x` as mutable + --> $DIR/issue-39544.rs:21:18 + | +21 | let _ = &mut z.x; + | ^^^ + +error: aborting due to previous error +