diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index facd60de6c28..74df7d8e58a1 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1415,24 +1415,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } + /// Return the dict-like variant corresponding to a given `Def`. pub fn def_struct_variant(&self, def: def::Def) -> Option<(ty::AdtDef<'tcx>, ty::VariantDef<'tcx>)> { - match def { + let (adt, variant) = match def { def::DefVariant(enum_id, variant_id, true) => { let adt = self.tcx().lookup_adt_def(enum_id); - Some((adt, adt.variant_with_id(variant_id))) + (adt, adt.variant_with_id(variant_id)) } def::DefTy(did, _) | def::DefStruct(did) => { let typ = self.tcx().lookup_item_type(did); if let ty::TyStruct(adt, _) = typ.ty.sty { - Some((adt, adt.struct_variant())) + (adt, adt.struct_variant()) } else { - None + return None; } } - _ => None + _ => return None + }; + + if let ty::VariantKind::Dict = variant.kind() { + Some((adt, variant)) + } else { + None } } diff --git a/src/test/compile-fail/issue-27831.rs b/src/test/compile-fail/issue-27831.rs new file mode 100644 index 000000000000..336368cf8a49 --- /dev/null +++ b/src/test/compile-fail/issue-27831.rs @@ -0,0 +1,34 @@ +// Copyright 2015 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. + +struct Foo(u32); +struct Bar; + +enum Enum { + Foo(u32), + Bar +} + +fn main() { + let x = Foo(1); + Foo { ..x }; //~ ERROR `Foo` does not name a structure + let Foo { .. } = x; //~ ERROR `Foo` does not name a struct + + let x = Bar; + Bar { ..x }; //~ ERROR `Bar` does not name a structure + let Bar { .. } = x; //~ ERROR `Bar` does not name a struct + + match Enum::Bar { + Enum::Bar { .. } //~ ERROR `Enum::Bar` does not name a struct + => {} + Enum::Foo { .. } //~ ERROR `Enum::Foo` does not name a struct + => {} + } +} diff --git a/src/test/compile-fail/issue-4736.rs b/src/test/compile-fail/issue-4736.rs index b63db7c5a30e..843ff38df49c 100644 --- a/src/test/compile-fail/issue-4736.rs +++ b/src/test/compile-fail/issue-4736.rs @@ -11,5 +11,5 @@ struct NonCopyable(()); fn main() { - let z = NonCopyable{ p: () }; //~ ERROR structure `NonCopyable` has no field named `p` + let z = NonCopyable{ p: () }; //~ ERROR `NonCopyable` does not name a structure }