Skip to content

Commit 8abc3a8

Browse files
authored
Rollup merge of rust-lang#120719 - compiler-errors:no-dyn-atb, r=lcnr
Remove support for `associated_type_bound` nested in `dyn` types These necessarily desugar to `impl Trait`, which is inconsistent with the `associated_type_bound` feature after rust-lang#120584. This PR keeps the `is_in_dyn_type` hack, which kind of makes me sad. Ideally, we'd be validating that no object types have associated type bounds somewhere else. Unfortunately, we can't do this later during astconv (i think?), nor can we do it earlier during ast validation (i think?) because of the feature gating of ATB being a *warning* rather than an *error*. Let me know if you have thoughts about this. r? lcnr
2 parents 48771e6 + e6f5af9 commit 8abc3a8

24 files changed

+91
-429
lines changed

compiler/rustc_ast_lowering/messages.ftl

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ ast_lowering_arbitrary_expression_in_pattern =
88
99
ast_lowering_argument = argument
1010
11+
ast_lowering_assoc_ty_binding_in_dyn =
12+
associated type bounds are not allowed in `dyn` types
13+
.suggestion = use `impl Trait` to introduce a type instead
14+
1115
ast_lowering_assoc_ty_parentheses =
1216
parenthesized generic arguments cannot be used in associated type constraints
1317
@@ -100,9 +104,6 @@ ast_lowering_match_arm_with_no_body =
100104
`match` arm with no body
101105
.suggestion = add a body after the pattern
102106
103-
ast_lowering_misplaced_assoc_ty_binding =
104-
associated type bounds are only allowed in where clauses and function signatures, not in {$position}
105-
106107
ast_lowering_misplaced_double_dot =
107108
`..` patterns are not allowed here
108109
.note = only allowed in tuple, tuple struct, and slice patterns

compiler/rustc_ast_lowering/src/errors.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ pub struct MisplacedImplTrait<'a> {
9494
}
9595

9696
#[derive(Diagnostic)]
97-
#[diag(ast_lowering_misplaced_assoc_ty_binding)]
98-
pub struct MisplacedAssocTyBinding<'a> {
97+
#[diag(ast_lowering_assoc_ty_binding_in_dyn)]
98+
pub struct MisplacedAssocTyBinding {
9999
#[primary_span]
100100
pub span: Span,
101-
pub position: DiagnosticArgFromDisplay<'a>,
101+
#[suggestion(code = " = impl", applicability = "maybe-incorrect", style = "verbose")]
102+
pub suggestion: Option<Span>,
102103
}
103104

104105
#[derive(Diagnostic, Clone, Copy)]

compiler/rustc_ast_lowering/src/lib.rs

+30-86
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ trait ResolverAstLoweringExt {
197197
fn get_label_res(&self, id: NodeId) -> Option<NodeId>;
198198
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
199199
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
200-
fn remap_extra_lifetime_params(&mut self, from: NodeId, to: NodeId);
201200
}
202201

203202
impl ResolverAstLoweringExt for ResolverAstLowering {
@@ -256,11 +255,6 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
256255
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
257256
self.extra_lifetime_params_map.remove(&id).unwrap_or_default()
258257
}
259-
260-
fn remap_extra_lifetime_params(&mut self, from: NodeId, to: NodeId) {
261-
let lifetimes = self.extra_lifetime_params_map.remove(&from).unwrap_or_default();
262-
self.extra_lifetime_params_map.insert(to, lifetimes);
263-
}
264258
}
265259

266260
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -1084,88 +1078,38 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10841078
hir::TypeBindingKind::Equality { term }
10851079
}
10861080
AssocConstraintKind::Bound { bounds } => {
1087-
enum DesugarKind {
1088-
ImplTrait,
1089-
Error(ImplTraitPosition),
1090-
Bound,
1091-
}
1092-
1093-
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
1094-
let desugar_kind = match itctx {
1095-
// in an argument, RPIT, or TAIT, if we are within a dyn type:
1096-
//
1097-
// fn foo(x: dyn Iterator<Item: Debug>)
1098-
//
1099-
// then desugar to:
1100-
//
1101-
// fn foo(x: dyn Iterator<Item = impl Debug>)
1102-
//
1103-
// This is because dyn traits must have all of their associated types specified.
1104-
ImplTraitContext::ReturnPositionOpaqueTy { .. }
1105-
| ImplTraitContext::TypeAliasesOpaqueTy { .. }
1106-
| ImplTraitContext::Universal
1107-
if self.is_in_dyn_type =>
1108-
{
1109-
DesugarKind::ImplTrait
1110-
}
1111-
1112-
ImplTraitContext::Disallowed(position) if self.is_in_dyn_type => {
1113-
DesugarKind::Error(position)
1114-
}
1115-
1116-
// We are in the parameter position, but not within a dyn type:
1117-
//
1118-
// fn foo(x: impl Iterator<Item: Debug>)
1119-
//
1120-
// so we leave it as is and this gets expanded in astconv to a bound like
1121-
// `<T as Iterator>::Item: Debug` where `T` is the type parameter for the
1122-
// `impl Iterator`.
1123-
_ => DesugarKind::Bound,
1124-
};
1125-
1126-
match desugar_kind {
1127-
DesugarKind::ImplTrait => {
1128-
// Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
1129-
// constructing the HIR for `impl bounds...` and then lowering that.
1130-
1131-
let impl_trait_node_id = self.next_node_id();
1132-
// Shift `impl Trait` lifetime captures from the associated type bound's
1133-
// node id to the opaque node id, so that the opaque can actually use
1134-
// these lifetime bounds.
1135-
self.resolver
1136-
.remap_extra_lifetime_params(constraint.id, impl_trait_node_id);
1137-
1138-
self.with_dyn_type_scope(false, |this| {
1139-
let node_id = this.next_node_id();
1140-
let ty = this.lower_ty(
1141-
&Ty {
1142-
id: node_id,
1143-
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
1144-
span: this.lower_span(constraint.span),
1145-
tokens: None,
1146-
},
1147-
itctx,
1148-
);
1081+
// Disallow ATB in dyn types
1082+
if self.is_in_dyn_type {
1083+
let suggestion = match itctx {
1084+
ImplTraitContext::ReturnPositionOpaqueTy { .. }
1085+
| ImplTraitContext::TypeAliasesOpaqueTy { .. }
1086+
| ImplTraitContext::Universal => {
1087+
let bound_end_span = constraint
1088+
.gen_args
1089+
.as_ref()
1090+
.map_or(constraint.ident.span, |args| args.span());
1091+
if bound_end_span.eq_ctxt(constraint.span) {
1092+
Some(self.tcx.sess.source_map().next_point(bound_end_span))
1093+
} else {
1094+
None
1095+
}
1096+
}
1097+
_ => None,
1098+
};
11491099

1150-
hir::TypeBindingKind::Equality { term: ty.into() }
1151-
})
1152-
}
1153-
DesugarKind::Bound => {
1154-
// Desugar `AssocTy: Bounds` into a type binding where the
1155-
// later desugars into a trait predicate.
1156-
let bounds = self.lower_param_bounds(bounds, itctx);
1100+
let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
1101+
span: constraint.span,
1102+
suggestion,
1103+
});
1104+
let err_ty =
1105+
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
1106+
hir::TypeBindingKind::Equality { term: err_ty.into() }
1107+
} else {
1108+
// Desugar `AssocTy: Bounds` into a type binding where the
1109+
// later desugars into a trait predicate.
1110+
let bounds = self.lower_param_bounds(bounds, itctx);
11571111

1158-
hir::TypeBindingKind::Constraint { bounds }
1159-
}
1160-
DesugarKind::Error(position) => {
1161-
let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
1162-
span: constraint.span,
1163-
position: DiagnosticArgFromDisplay(&position),
1164-
});
1165-
let err_ty =
1166-
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
1167-
hir::TypeBindingKind::Equality { term: err_ty.into() }
1168-
}
1112+
hir::TypeBindingKind::Constraint { bounds }
11691113
}
11701114
}
11711115
};

tests/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ impl Bar for AssocNoCopy {
2828

2929
impl Thing for AssocNoCopy {
3030
type Out = Box<dyn Bar<Assoc: Copy>>;
31+
//~^ ERROR associated type bounds are not allowed in `dyn` types
3132

3233
fn func() -> Self::Out {
33-
//~^ ERROR the trait bound `String: Copy` is not satisfied
3434
Box::new(AssocNoCopy)
3535
}
3636
}

tests/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.stderr

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
error[E0277]: the trait bound `String: Copy` is not satisfied
2-
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:32:18
1+
error: associated type bounds are not allowed in `dyn` types
2+
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:30:28
33
|
4-
LL | fn func() -> Self::Out {
5-
| ^^^^^^^^^ the trait `Copy` is not implemented for `String`
4+
LL | type Out = Box<dyn Bar<Assoc: Copy>>;
5+
| ^^^^^^^^^^^
6+
|
7+
help: use `impl Trait` to introduce a type instead
8+
|
9+
LL | type Out = Box<dyn Bar<Assoc = impl Copy>>;
10+
| ~~~~~~
611

712
error: aborting due to 1 previous error
813

9-
For more information about this error, try `rustc --explain E0277`.

tests/ui/associated-type-bounds/bad-universal-in-dyn-in-where-clause.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait B {
77
fn f()
88
where
99
dyn for<'j> B<AssocType: 'j>:,
10-
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
10+
//~^ ERROR associated type bounds are not allowed in `dyn` types
1111
{
1212
}
1313

tests/ui/associated-type-bounds/bad-universal-in-dyn-in-where-clause.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: associated type bounds are only allowed in where clauses and function signatures, not in bounds
1+
error: associated type bounds are not allowed in `dyn` types
22
--> $DIR/bad-universal-in-dyn-in-where-clause.rs:9:19
33
|
44
LL | dyn for<'j> B<AssocType: 'j>:,

tests/ui/associated-type-bounds/bad-universal-in-impl-sig.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ trait Trait2 {}
88

99
// It's not possible to insert a universal `impl Trait` here!
1010
impl dyn Trait<Item: Trait2> {}
11-
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
11+
//~^ ERROR associated type bounds are not allowed in `dyn` types
1212

1313
fn main() {}

tests/ui/associated-type-bounds/bad-universal-in-impl-sig.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: associated type bounds are only allowed in where clauses and function signatures, not in impl headers
1+
error: associated type bounds are not allowed in `dyn` types
22
--> $DIR/bad-universal-in-impl-sig.rs:10:16
33
|
44
LL | impl dyn Trait<Item: Trait2> {}

tests/ui/associated-type-bounds/duplicate.rs

-7
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,4 @@ trait TRA3 {
261261
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
262262
}
263263

264-
type TADyn1 = dyn Iterator<Item: Copy, Item: Send>;
265-
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
266-
type TADyn2 = Box<dyn Iterator<Item: Copy, Item: Copy>>;
267-
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
268-
type TADyn3 = dyn Iterator<Item: 'static, Item: 'static>;
269-
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
270-
271264
fn main() {}

tests/ui/associated-type-bounds/duplicate.stderr

+1-25
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,6 @@ LL | struct SI1<T: Iterator<Item: Copy, Item: Send>> {
66
| |
77
| `Item` bound here first
88

9-
error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
10-
--> $DIR/duplicate.rs:264:40
11-
|
12-
LL | type TADyn1 = dyn Iterator<Item: Copy, Item: Send>;
13-
| ---------- ^^^^^^^^^^ re-bound here
14-
| |
15-
| `Item` bound here first
16-
17-
error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
18-
--> $DIR/duplicate.rs:266:44
19-
|
20-
LL | type TADyn2 = Box<dyn Iterator<Item: Copy, Item: Copy>>;
21-
| ---------- ^^^^^^^^^^ re-bound here
22-
| |
23-
| `Item` bound here first
24-
25-
error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
26-
--> $DIR/duplicate.rs:268:43
27-
|
28-
LL | type TADyn3 = dyn Iterator<Item: 'static, Item: 'static>;
29-
| ------------- ^^^^^^^^^^^^^ re-bound here
30-
| |
31-
| `Item` bound here first
32-
339
error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
3410
--> $DIR/duplicate.rs:11:36
3511
|
@@ -631,7 +607,7 @@ LL | Self: Iterator<Item: 'static, Item: 'static>,
631607
|
632608
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
633609

634-
error: aborting due to 75 previous errors
610+
error: aborting due to 72 previous errors
635611

636612
Some errors have detailed explanations: E0282, E0719.
637613
For more information about an error, try `rustc --explain E0282`.

tests/ui/associated-type-bounds/dyn-impl-trait-type.rs

-66
This file was deleted.

tests/ui/associated-type-bounds/dyn-impl-trait-type.stderr

-12
This file was deleted.

0 commit comments

Comments
 (0)