From 243e45aac3fccf9403ad1bed162073f4f314b270 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Sun, 4 Dec 2016 02:04:08 +0200 Subject: [PATCH] normalize field types in copy implementations Fixes #34377. --- src/librustc/ty/util.rs | 52 ++++++++++++++++++-------------- src/test/run-pass/issue-33187.rs | 27 +++++++++++++++++ 2 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 src/test/run-pass/issue-33187.rs diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index 0b45ff94a9312..34c07d442e33d 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -147,34 +147,40 @@ impl<'tcx> ParameterEnvironment<'tcx> { self_type: Ty<'tcx>, span: Span) -> Result<(),CopyImplementationError> { // FIXME: (@jroesch) float this code up - tcx.infer_ctxt(None, Some(self.clone()), Reveal::ExactMatch).enter(|infcx| { - let adt = match self_type.sty { - ty::TyAdt(adt, substs) => match adt.adt_kind() { - AdtKind::Struct | AdtKind::Union => { - for field in adt.all_fields() { - let field_ty = field.ty(tcx, substs); - if infcx.type_moves_by_default(field_ty, span) { - return Err(CopyImplementationError::InfrigingField( - field.name)) - } + tcx.infer_ctxt(None, Some(self.clone()), Reveal::NotSpecializable).enter(|infcx| { + let (adt, substs) = match self_type.sty { + ty::TyAdt(adt, substs) => (adt, substs), + _ => return Err(CopyImplementationError::NotAnAdt) + }; + + let field_implements_copy = |field: &ty::FieldDef| { + let cause = traits::ObligationCause::dummy(); + match traits::fully_normalize(&infcx, cause, &field.ty(tcx, substs)) { + Ok(ty) => !infcx.type_moves_by_default(ty, span), + Err(..) => false + } + }; + + match adt.adt_kind() { + AdtKind::Struct | AdtKind::Union => { + for field in adt.all_fields() { + if !field_implements_copy(field) { + return Err(CopyImplementationError::InfrigingField( + field.name)) } - adt } - AdtKind::Enum => { - for variant in &adt.variants { - for field in &variant.fields { - let field_ty = field.ty(tcx, substs); - if infcx.type_moves_by_default(field_ty, span) { - return Err(CopyImplementationError::InfrigingVariant( - variant.name)) - } + } + AdtKind::Enum => { + for variant in &adt.variants { + for field in &variant.fields { + if !field_implements_copy(field) { + return Err(CopyImplementationError::InfrigingVariant( + variant.name)) } } - adt } - }, - _ => return Err(CopyImplementationError::NotAnAdt) - }; + } + } if adt.has_dtor() { return Err(CopyImplementationError::HasDestructor); diff --git a/src/test/run-pass/issue-33187.rs b/src/test/run-pass/issue-33187.rs new file mode 100644 index 0000000000000..477112ab3c5ab --- /dev/null +++ b/src/test/run-pass/issue-33187.rs @@ -0,0 +1,27 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Foo(::Data); + +impl Copy for Foo where ::Data: Copy { } +impl Clone for Foo where ::Data: Clone { + fn clone(&self) -> Self { Foo(self.0.clone()) } +} + +trait Repr { + type Data; +} + +impl Repr for A { + type Data = u32; +} + +fn main() { +}