From df73bc9c9d550c2517edc3cf49164d9e93c5b152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 6 Feb 2017 15:41:28 -0800 Subject: [PATCH] Fix ICE when accessing mutably an immutable enum --- src/librustc/middle/mem_categorization.rs | 4 +++- src/test/ui/did_you_mean/issue-39544.rs | 22 +++++++++++++++++++++ src/test/ui/did_you_mean/issue-39544.stderr | 8 ++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/did_you_mean/issue-39544.rs create mode 100644 src/test/ui/did_you_mean/issue-39544.stderr 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 +