Skip to content

Commit 243e45a

Browse files
committed
normalize field types in copy implementations
Fixes #34377.
1 parent d938ba4 commit 243e45a

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

src/librustc/ty/util.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,34 +147,40 @@ impl<'tcx> ParameterEnvironment<'tcx> {
147147
self_type: Ty<'tcx>, span: Span)
148148
-> Result<(),CopyImplementationError> {
149149
// FIXME: (@jroesch) float this code up
150-
tcx.infer_ctxt(None, Some(self.clone()), Reveal::ExactMatch).enter(|infcx| {
151-
let adt = match self_type.sty {
152-
ty::TyAdt(adt, substs) => match adt.adt_kind() {
153-
AdtKind::Struct | AdtKind::Union => {
154-
for field in adt.all_fields() {
155-
let field_ty = field.ty(tcx, substs);
156-
if infcx.type_moves_by_default(field_ty, span) {
157-
return Err(CopyImplementationError::InfrigingField(
158-
field.name))
159-
}
150+
tcx.infer_ctxt(None, Some(self.clone()), Reveal::NotSpecializable).enter(|infcx| {
151+
let (adt, substs) = match self_type.sty {
152+
ty::TyAdt(adt, substs) => (adt, substs),
153+
_ => return Err(CopyImplementationError::NotAnAdt)
154+
};
155+
156+
let field_implements_copy = |field: &ty::FieldDef| {
157+
let cause = traits::ObligationCause::dummy();
158+
match traits::fully_normalize(&infcx, cause, &field.ty(tcx, substs)) {
159+
Ok(ty) => !infcx.type_moves_by_default(ty, span),
160+
Err(..) => false
161+
}
162+
};
163+
164+
match adt.adt_kind() {
165+
AdtKind::Struct | AdtKind::Union => {
166+
for field in adt.all_fields() {
167+
if !field_implements_copy(field) {
168+
return Err(CopyImplementationError::InfrigingField(
169+
field.name))
160170
}
161-
adt
162171
}
163-
AdtKind::Enum => {
164-
for variant in &adt.variants {
165-
for field in &variant.fields {
166-
let field_ty = field.ty(tcx, substs);
167-
if infcx.type_moves_by_default(field_ty, span) {
168-
return Err(CopyImplementationError::InfrigingVariant(
169-
variant.name))
170-
}
172+
}
173+
AdtKind::Enum => {
174+
for variant in &adt.variants {
175+
for field in &variant.fields {
176+
if !field_implements_copy(field) {
177+
return Err(CopyImplementationError::InfrigingVariant(
178+
variant.name))
171179
}
172180
}
173-
adt
174181
}
175-
},
176-
_ => return Err(CopyImplementationError::NotAnAdt)
177-
};
182+
}
183+
}
178184

179185
if adt.has_dtor() {
180186
return Err(CopyImplementationError::HasDestructor);

src/test/run-pass/issue-33187.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo<A: Repr>(<A as Repr>::Data);
12+
13+
impl<A> Copy for Foo<A> where <A as Repr>::Data: Copy { }
14+
impl<A> Clone for Foo<A> where <A as Repr>::Data: Clone {
15+
fn clone(&self) -> Self { Foo(self.0.clone()) }
16+
}
17+
18+
trait Repr {
19+
type Data;
20+
}
21+
22+
impl<A> Repr for A {
23+
type Data = u32;
24+
}
25+
26+
fn main() {
27+
}

0 commit comments

Comments
 (0)