Skip to content

Commit 8265592

Browse files
committed
Make GATs object safe under generic_associated_types_extended feature
1 parent a0ef4bf commit 8265592

19 files changed

+233
-54
lines changed

Cargo.lock

+10-10
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ dependencies = [
320320
"cargo-test-macro",
321321
"cargo-test-support",
322322
"cargo-util",
323-
"clap 3.1.6",
323+
"clap 3.1.1",
324324
"crates-io",
325325
"crossbeam-utils",
326326
"curl",
@@ -598,17 +598,17 @@ dependencies = [
598598

599599
[[package]]
600600
name = "clap"
601-
version = "3.1.6"
601+
version = "3.1.1"
602602
source = "registry+https://github.com/rust-lang/crates.io-index"
603-
checksum = "d8c93436c21e4698bacadf42917db28b23017027a4deccb35dbe47a7e7840123"
603+
checksum = "6d76c22c9b9b215eeb8d016ad3a90417bd13cb24cf8142756e6472445876cab7"
604604
dependencies = [
605605
"atty",
606606
"bitflags",
607607
"indexmap",
608608
"os_str_bytes",
609609
"strsim 0.10.0",
610610
"termcolor",
611-
"textwrap 0.15.0",
611+
"textwrap 0.14.2",
612612
]
613613

614614
[[package]]
@@ -1502,9 +1502,9 @@ dependencies = [
15021502

15031503
[[package]]
15041504
name = "git2"
1505-
version = "0.14.2"
1505+
version = "0.14.1"
15061506
source = "registry+https://github.com/rust-lang/crates.io-index"
1507-
checksum = "3826a6e0e2215d7a41c2bfc7c9244123969273f3476b939a226aac0ab56e9e3c"
1507+
checksum = "6e7d3b96ec1fcaa8431cf04a4f1ef5caafe58d5cf7bcc31f09c1626adddb0ffe"
15081508
dependencies = [
15091509
"bitflags",
15101510
"libc",
@@ -1974,9 +1974,9 @@ dependencies = [
19741974

19751975
[[package]]
19761976
name = "libgit2-sys"
1977-
version = "0.13.2+1.4.2"
1977+
version = "0.13.1+1.4.2"
19781978
source = "registry+https://github.com/rust-lang/crates.io-index"
1979-
checksum = "3a42de9a51a5c12e00fc0e4ca6bc2ea43582fc6418488e8f615e905d886f258b"
1979+
checksum = "43e598aa7a4faedf1ea1b4608f582b06f0f40211eec551b7ef36019ae3f62def"
19801980
dependencies = [
19811981
"cc",
19821982
"libc",
@@ -5024,9 +5024,9 @@ dependencies = [
50245024

50255025
[[package]]
50265026
name = "textwrap"
5027-
version = "0.15.0"
5027+
version = "0.14.2"
50285028
source = "registry+https://github.com/rust-lang/crates.io-index"
5029-
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
5029+
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
50305030

50315031
[[package]]
50325032
name = "thiserror"

compiler/rustc_trait_selection/src/traits/object_safety.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,18 @@ fn object_safety_violations_for_trait(
131131
}),
132132
);
133133

134-
violations.extend(
135-
tcx.associated_items(trait_def_id)
136-
.in_definition_order()
137-
.filter(|item| item.kind == ty::AssocKind::Type)
138-
.filter(|item| !tcx.generics_of(item.def_id).params.is_empty())
139-
.map(|item| {
140-
let ident = item.ident(tcx);
141-
ObjectSafetyViolation::GAT(ident.name, ident.span)
142-
}),
143-
);
134+
if !tcx.features().generic_associated_types_extended {
135+
violations.extend(
136+
tcx.associated_items(trait_def_id)
137+
.in_definition_order()
138+
.filter(|item| item.kind == ty::AssocKind::Type)
139+
.filter(|item| !tcx.generics_of(item.def_id).params.is_empty())
140+
.map(|item| {
141+
let ident = item.ident(tcx);
142+
ObjectSafetyViolation::GAT(ident.name, ident.span)
143+
}),
144+
);
145+
}
144146

145147
debug!(
146148
"object_safety_violations_for_trait(trait_def_id={:?}) = {:?}",

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+66-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use rustc_hir::Constness;
1212
use rustc_index::bit_set::GrowableBitSet;
1313
use rustc_infer::infer::InferOk;
1414
use rustc_infer::infer::LateBoundRegionConversionTime::HigherRankedType;
15-
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef};
16-
use rustc_middle::ty::{self, Ty};
15+
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef};
16+
use rustc_middle::ty::{self, GenericParamDefKind, Ty};
1717
use rustc_middle::ty::{ToPolyTraitRef, ToPredicate};
1818
use rustc_span::def_id::DefId;
1919

@@ -494,18 +494,80 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
494494
.collect();
495495

496496
for assoc_type in assoc_types {
497-
if !tcx.generics_of(assoc_type).params.is_empty() {
497+
let defs: &ty::Generics = tcx.generics_of(assoc_type);
498+
499+
if !defs.params.is_empty() && !tcx.features().generic_associated_types_extended {
498500
tcx.sess.delay_span_bug(
499501
obligation.cause.span,
500502
"GATs in trait object shouldn't have been considered",
501503
);
502504
return Err(SelectionError::Unimplemented);
503505
}
506+
504507
// This maybe belongs in wf, but that can't (doesn't) handle
505508
// higher-ranked things.
506509
// Prevent, e.g., `dyn Iterator<Item = str>`.
507510
for bound in self.tcx().item_bounds(assoc_type) {
508-
let subst_bound = bound.subst(tcx, trait_predicate.trait_ref.substs);
511+
let subst_bound =
512+
if defs.count() == 0 {
513+
bound.subst(tcx, trait_predicate.trait_ref.substs)
514+
} else {
515+
let mut substs = smallvec::SmallVec::with_capacity(defs.count());
516+
substs.extend(trait_predicate.trait_ref.substs.iter());
517+
let mut bound_vars: smallvec::SmallVec<[ty::BoundVariableKind; 8]> =
518+
smallvec::SmallVec::with_capacity(
519+
bound.kind().bound_vars().len() + defs.count(),
520+
);
521+
bound_vars.extend(bound.kind().bound_vars().into_iter());
522+
InternalSubsts::fill_single(&mut substs, defs, &mut |param, _| match param
523+
.kind
524+
{
525+
GenericParamDefKind::Type { .. } => {
526+
let kind = ty::BoundTyKind::Param(param.name);
527+
let bound_var = ty::BoundVariableKind::Ty(kind);
528+
bound_vars.push(bound_var);
529+
tcx.mk_ty(ty::Bound(
530+
ty::INNERMOST,
531+
ty::BoundTy {
532+
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
533+
kind,
534+
},
535+
))
536+
.into()
537+
}
538+
GenericParamDefKind::Lifetime => {
539+
let kind = ty::BoundRegionKind::BrNamed(param.def_id, param.name);
540+
let bound_var = ty::BoundVariableKind::Region(kind);
541+
bound_vars.push(bound_var);
542+
tcx.mk_region(ty::ReLateBound(
543+
ty::INNERMOST,
544+
ty::BoundRegion {
545+
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
546+
kind,
547+
},
548+
))
549+
.into()
550+
}
551+
GenericParamDefKind::Const { .. } => {
552+
let bound_var = ty::BoundVariableKind::Const;
553+
bound_vars.push(bound_var);
554+
tcx.mk_const(ty::ConstS {
555+
ty: tcx.type_of(param.def_id),
556+
val: ty::ConstKind::Bound(
557+
ty::INNERMOST,
558+
ty::BoundVar::from_usize(bound_vars.len() - 1),
559+
),
560+
})
561+
.into()
562+
}
563+
});
564+
let bound_vars = tcx.mk_bound_variable_kinds(bound_vars.into_iter());
565+
let assoc_ty_substs = tcx.intern_substs(&substs);
566+
567+
let bound_vars = tcx.mk_bound_variable_kinds(bound_vars.into_iter());
568+
let bound = bound.kind().skip_binder().subst(tcx, assoc_ty_substs);
569+
tcx.mk_predicate(ty::Binder::bind_with_vars(bound, bound_vars))
570+
};
509571
let normalized_bound = normalize_with_depth_to(
510572
self,
511573
obligation.param_env,

src/test/ui/generic-associated-types/gat-in-trait-path.stderr renamed to src/test/ui/generic-associated-types/gat-in-trait-path.base.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0038]: the trait `Foo` cannot be made into an object
2-
--> $DIR/gat-in-trait-path.rs:21:17
2+
--> $DIR/gat-in-trait-path.rs:27:17
33
|
44
LL | fn f(_arg : Box<dyn for<'a> Foo<A<'a> = &'a ()>>) {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
66
|
77
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
8-
--> $DIR/gat-in-trait-path.rs:5:10
8+
--> $DIR/gat-in-trait-path.rs:11:10
99
|
1010
LL | trait Foo {
1111
| --- this trait cannot be made into an object...

src/test/ui/generic-associated-types/gat-in-trait-path.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
// revisions: base extended
2+
//[base] check-fail
3+
//[extended] check-pass
4+
15
#![feature(generic_associated_types)]
26
#![feature(associated_type_defaults)]
7+
#![cfg_attr(extended, feature(generic_associated_types_extended))]
8+
#![cfg_attr(extended, allow(incomplete_features))]
39

410
trait Foo {
511
type A<'a> where Self: 'a;
@@ -19,7 +25,7 @@ impl<T> Foo for Fooer<T> {
1925
}
2026

2127
fn f(_arg : Box<dyn for<'a> Foo<A<'a> = &'a ()>>) {}
22-
//~^ the trait `Foo` cannot be made into an object
28+
//[base]~^ the trait `Foo` cannot be made into an object
2329

2430

2531
fn main() {

src/test/ui/generic-associated-types/issue-67510-pass.stderr renamed to src/test/ui/generic-associated-types/issue-67510-pass.base.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0038]: the trait `X` cannot be made into an object
2-
--> $DIR/issue-67510-pass.rs:7:23
2+
--> $DIR/issue-67510-pass.rs:13:23
33
|
44
LL | fn _func1<'a>(_x: Box<dyn X<Y<'a>=&'a ()>>) {}
55
| ^^^^^^^^^^^^^^^^^^^ `X` cannot be made into an object
66
|
77
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
8-
--> $DIR/issue-67510-pass.rs:4:10
8+
--> $DIR/issue-67510-pass.rs:10:10
99
|
1010
LL | trait X {
1111
| - this trait cannot be made into an object...
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
// revisions: base extended
2+
//[base] check-fail
3+
//[extended] check-pass
4+
15
#![feature(generic_associated_types)]
6+
#![cfg_attr(extended, feature(generic_associated_types_extended))]
7+
#![cfg_attr(extended, allow(incomplete_features))]
28

39
trait X {
410
type Y<'a>;
511
}
612

713
fn _func1<'a>(_x: Box<dyn X<Y<'a>=&'a ()>>) {}
8-
//~^ ERROR the trait `X` cannot be made into an object
14+
//[base]~^ ERROR the trait `X` cannot be made into an object
915

1016
fn main() {}

src/test/ui/generic-associated-types/issue-76535.stderr renamed to src/test/ui/generic-associated-types/issue-76535.base.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0107]: missing generics for associated type `SuperTrait::SubType`
2-
--> $DIR/issue-76535.rs:36:33
2+
--> $DIR/issue-76535.rs:40:33
33
|
44
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
55
| ^^^^^^^ expected 1 lifetime argument
66
|
77
note: associated type defined here, with 1 lifetime parameter: `'a`
8-
--> $DIR/issue-76535.rs:6:10
8+
--> $DIR/issue-76535.rs:10:10
99
|
1010
LL | type SubType<'a>: SubTrait where Self: 'a;
1111
| ^^^^^^^ --
@@ -15,13 +15,13 @@ LL | let sub: Box<dyn SuperTrait<SubType<'a> = SubStruct>> = Box::new(SuperS
1515
| ~~~~~~~~~~~
1616

1717
error[E0038]: the trait `SuperTrait` cannot be made into an object
18-
--> $DIR/issue-76535.rs:36:14
18+
--> $DIR/issue-76535.rs:40:14
1919
|
2020
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` cannot be made into an object
2222
|
2323
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
24-
--> $DIR/issue-76535.rs:6:10
24+
--> $DIR/issue-76535.rs:10:10
2525
|
2626
LL | pub trait SuperTrait {
2727
| ---------- this trait cannot be made into an object...
@@ -30,13 +30,13 @@ LL | type SubType<'a>: SubTrait where Self: 'a;
3030
= help: consider moving `SubType` to another trait
3131

3232
error[E0038]: the trait `SuperTrait` cannot be made into an object
33-
--> $DIR/issue-76535.rs:36:57
33+
--> $DIR/issue-76535.rs:40:57
3434
|
3535
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
3636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` cannot be made into an object
3737
|
3838
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
39-
--> $DIR/issue-76535.rs:6:10
39+
--> $DIR/issue-76535.rs:10:10
4040
|
4141
LL | pub trait SuperTrait {
4242
| ---------- this trait cannot be made into an object...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0107]: missing generics for associated type `SuperTrait::SubType`
2+
--> $DIR/issue-76535.rs:40:33
3+
|
4+
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
5+
| ^^^^^^^ expected 1 lifetime argument
6+
|
7+
note: associated type defined here, with 1 lifetime parameter: `'a`
8+
--> $DIR/issue-76535.rs:10:10
9+
|
10+
LL | type SubType<'a>: SubTrait where Self: 'a;
11+
| ^^^^^^^ --
12+
help: add missing lifetime argument
13+
|
14+
LL | let sub: Box<dyn SuperTrait<SubType<'a> = SubStruct>> = Box::new(SuperStruct::new(0));
15+
| ~~~~~~~~~~~
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0107`.

src/test/ui/generic-associated-types/issue-76535.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
// revisions: base extended
2+
13
#![feature(generic_associated_types)]
4+
#![cfg_attr(extended, feature(generic_associated_types_extended))]
5+
#![cfg_attr(extended, allow(incomplete_features))]
26

37
pub trait SubTrait {}
48

@@ -35,6 +39,6 @@ impl SuperTrait for SuperStruct {
3539
fn main() {
3640
let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
3741
//~^ ERROR missing generics for associated type
38-
//~^^ ERROR the trait
39-
//~| ERROR the trait
42+
//[base]~^^ ERROR the trait
43+
//[base]~| ERROR the trait
4044
}

src/test/ui/generic-associated-types/issue-78671.stderr renamed to src/test/ui/generic-associated-types/issue-78671.base.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0107]: missing generics for associated type `CollectionFamily::Member`
2-
--> $DIR/issue-78671.rs:7:47
2+
--> $DIR/issue-78671.rs:11:47
33
|
44
LL | Box::new(Family) as &dyn CollectionFamily<Member=usize>
55
| ^^^^^^ expected 1 generic argument
66
|
77
note: associated type defined here, with 1 generic parameter: `T`
8-
--> $DIR/issue-78671.rs:4:10
8+
--> $DIR/issue-78671.rs:8:10
99
|
1010
LL | type Member<T>;
1111
| ^^^^^^ -
@@ -15,13 +15,13 @@ LL | Box::new(Family) as &dyn CollectionFamily<Member<T>=usize>
1515
| ~~~~~~~~~
1616

1717
error[E0038]: the trait `CollectionFamily` cannot be made into an object
18-
--> $DIR/issue-78671.rs:7:25
18+
--> $DIR/issue-78671.rs:11:25
1919
|
2020
LL | Box::new(Family) as &dyn CollectionFamily<Member=usize>
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `CollectionFamily` cannot be made into an object
2222
|
2323
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
24-
--> $DIR/issue-78671.rs:4:10
24+
--> $DIR/issue-78671.rs:8:10
2525
|
2626
LL | trait CollectionFamily {
2727
| ---------------- this trait cannot be made into an object...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0107]: missing generics for associated type `CollectionFamily::Member`
2+
--> $DIR/issue-78671.rs:11:47
3+
|
4+
LL | Box::new(Family) as &dyn CollectionFamily<Member=usize>
5+
| ^^^^^^ expected 1 generic argument
6+
|
7+
note: associated type defined here, with 1 generic parameter: `T`
8+
--> $DIR/issue-78671.rs:8:10
9+
|
10+
LL | type Member<T>;
11+
| ^^^^^^ -
12+
help: add missing generic argument
13+
|
14+
LL | Box::new(Family) as &dyn CollectionFamily<Member<T>=usize>
15+
| ~~~~~~~~~
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)