Skip to content

Commit 333c6bf

Browse files
copy self type is implied wf
1 parent 8cf7f40 commit 333c6bf

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

compiler/rustc_trait_selection/src/traits/misc.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
33
use crate::traits::{self, ObligationCause};
44

5+
use rustc_data_structures::fx::FxIndexSet;
56
use rustc_hir as hir;
67
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
78
use rustc_infer::infer::TyCtxtInferExt;
89
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable};
910

1011
use crate::traits::error_reporting::TypeErrCtxtExt;
1112

13+
use super::outlives_bounds::InferCtxtExt;
14+
1215
#[derive(Clone)]
1316
pub enum CopyImplementationError<'tcx> {
1417
InfrigingFields(Vec<(&'tcx ty::FieldDef, Ty<'tcx>)>),
@@ -45,6 +48,7 @@ pub fn type_allowed_to_implement_copy<'tcx>(
4548
};
4649

4750
let copy_def_id = tcx.require_lang_item(hir::LangItem::Copy, Some(parent_cause.span));
51+
4852
let mut infringing = Vec::new();
4953
for variant in adt.variants() {
5054
for field in &variant.fields {
@@ -85,7 +89,16 @@ pub fn type_allowed_to_implement_copy<'tcx>(
8589
infringing.push((field, ty));
8690
}
8791

88-
let outlives_env = OutlivesEnvironment::new(param_env);
92+
// Check regions assuming the self type of the impl is WF
93+
let outlives_env = OutlivesEnvironment::with_bounds(
94+
param_env,
95+
Some(&infcx),
96+
infcx.implied_bounds_tys(
97+
param_env,
98+
parent_cause.body_id,
99+
FxIndexSet::from_iter([self_type]),
100+
),
101+
);
89102
infcx.process_registered_region_obligations(
90103
outlives_env.region_bound_pairs(),
91104
param_env,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
3+
#[derive(Clone)]
4+
struct A<'a, T>(&'a T);
5+
6+
impl<'a, T: Copy + 'a> Copy for A<'a, T> {}
7+
8+
#[derive(Clone)]
9+
struct B<'a, T>(A<'a, T>);
10+
11+
// `T: '_` should be implied by `WF(B<'_, T>)`.
12+
impl<T: Copy> Copy for B<'_, T> {}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)