1
Fork 0

Move associated type bounds check to ast lowering

This makes the check for when associated type bounds more accurate
This commit is contained in:
Michael Goulet 2023-02-14 19:15:43 +00:00
parent b869e84e58
commit e7c490892f
12 changed files with 171 additions and 139 deletions

View file

@ -19,6 +19,9 @@ ast_lowering_remove_parentheses = remove these parentheses
ast_lowering_misplaced_impl_trait =
`impl Trait` only allowed in function and inherent method return types, not in {$position}
ast_lowering_misplaced_assoc_ty_binding =
associated type bounds are only allowed in where clauses and function signatures, not in {$position}
ast_lowering_rustc_box_attribute_error =
#[rustc_box] requires precisely one argument and no other attributes are allowed

View file

@ -79,6 +79,14 @@ pub struct MisplacedImplTrait<'a> {
pub position: DiagnosticArgFromDisplay<'a>,
}
#[derive(Diagnostic)]
#[diag(ast_lowering_misplaced_assoc_ty_binding)]
pub struct MisplacedAssocTyBinding<'a> {
#[primary_span]
pub span: Span,
pub position: DiagnosticArgFromDisplay<'a>,
}
#[derive(Diagnostic, Clone, Copy)]
#[diag(ast_lowering_rustc_box_attribute_error)]
pub struct RustcBoxAttributeError {

View file

@ -1002,8 +1002,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} else {
self.arena.alloc(hir::GenericArgs::none())
};
let itctx_tait = &ImplTraitContext::TypeAliasesOpaqueTy;
let kind = match &constraint.kind {
AssocConstraintKind::Equality { term } => {
let term = match term {
@ -1040,7 +1038,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// then to an opaque type).
//
// FIXME: this is only needed until `impl Trait` is allowed in type aliases.
ImplTraitContext::Disallowed(_) if self.is_in_dyn_type => (true, itctx_tait),
ImplTraitContext::Disallowed(position) if self.is_in_dyn_type => {
self.tcx.sess.emit_err(errors::MisplacedAssocTyBinding {
span: constraint.span,
position: DiagnosticArgFromDisplay(position),
});
(false, itctx)
}
// We are in the parameter position, but not within a dyn type:
//

View file

@ -11,9 +11,6 @@ ast_passes_forbidden_let_stable =
ast_passes_deprecated_where_clause_location =
where clause not allowed here
ast_passes_forbidden_assoc_constraint =
associated type bounds are not allowed within structs, enums, or unions
ast_passes_keyword_lifetime =
lifetimes cannot use keyword names

View file

@ -71,10 +71,6 @@ struct AstValidator<'a> {
/// or `Foo::Bar<impl Trait>`
is_impl_trait_banned: bool,
/// Used to ban associated type bounds (i.e., `Type<AssocType: Bounds>`) in
/// certain positions.
is_assoc_ty_bound_banned: bool,
/// See [ForbiddenLetReason]
forbidden_let_reason: Option<ForbiddenLetReason>,
@ -180,30 +176,12 @@ impl<'a> AstValidator<'a> {
}
}
fn with_banned_assoc_ty_bound(&mut self, f: impl FnOnce(&mut Self)) {
let old = mem::replace(&mut self.is_assoc_ty_bound_banned, true);
f(self);
self.is_assoc_ty_bound_banned = old;
}
fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
let old = mem::replace(&mut self.outer_impl_trait, outer);
f(self);
self.outer_impl_trait = old;
}
fn visit_assoc_constraint_from_generic_args(&mut self, constraint: &'a AssocConstraint) {
match constraint.kind {
AssocConstraintKind::Equality { .. } => {}
AssocConstraintKind::Bound { .. } => {
if self.is_assoc_ty_bound_banned {
self.session.emit_err(ForbiddenAssocConstraint { span: constraint.span });
}
}
}
self.visit_assoc_constraint(constraint);
}
// Mirrors `visit::walk_ty`, but tracks relevant state.
fn walk_ty(&mut self, t: &'a Ty) {
match &t.kind {
@ -1248,7 +1226,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
// are allowed to contain nested `impl Trait`.
AngleBracketedArg::Constraint(constraint) => {
self.with_impl_trait(None, |this| {
this.visit_assoc_constraint_from_generic_args(constraint);
this.visit_assoc_constraint(constraint);
});
}
}
@ -1373,14 +1351,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
visit::walk_param_bound(self, bound)
}
fn visit_variant_data(&mut self, s: &'a VariantData) {
self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s))
}
fn visit_enum_def(&mut self, enum_definition: &'a EnumDef) {
self.with_banned_assoc_ty_bound(|this| visit::walk_enum_def(this, enum_definition))
}
fn visit_fn(&mut self, fk: FnKind<'a>, span: Span, id: NodeId) {
// Only associated `fn`s can have `self` parameters.
let self_semantic = match fk.ctxt() {
@ -1709,7 +1679,6 @@ pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) ->
outer_impl_trait: None,
disallow_tilde_const: None,
is_impl_trait_banned: false,
is_assoc_ty_bound_banned: false,
forbidden_let_reason: Some(ForbiddenLetReason::GenericForbidden),
lint_buffer: lints,
};

View file

@ -23,13 +23,6 @@ pub struct ForbiddenLetStable {
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(ast_passes_forbidden_assoc_constraint)]
pub struct ForbiddenAssocConstraint {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(ast_passes_keyword_lifetime)]
pub struct KeywordLifetime {

View file

@ -0,0 +1,15 @@
#![feature(associated_type_bounds)]
trait B {
type AssocType;
}
fn f()
where
dyn for<'j> B<AssocType: 'j>:,
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `AssocType` (from trait `B`) must be specified
{
}
fn main() {}

View file

@ -0,0 +1,18 @@
error: associated type bounds are only allowed in where clauses and function signatures, not in bound
--> $DIR/bad-universal-in-dyn-in-where-clause.rs:9:19
|
LL | dyn for<'j> B<AssocType: 'j>:,
| ^^^^^^^^^^^^^
error[E0191]: the value of the associated type `AssocType` (from trait `B`) must be specified
--> $DIR/bad-universal-in-dyn-in-where-clause.rs:9:9
|
LL | type AssocType;
| -------------- `AssocType` defined here
...
LL | dyn for<'j> B<AssocType: 'j>:,
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: specify the associated type: `for<'j> B<AssocType: 'j, AssocType = Type>`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0191`.

View file

@ -0,0 +1,14 @@
#![feature(associated_type_bounds)]
trait Trait {
type Item;
}
trait Trait2 {}
// It's not possible to insert a universal `impl Trait` here!
impl dyn Trait<Item: Trait2> {}
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Trait`) must be specified
fn main() {}

View file

@ -0,0 +1,8 @@
error: associated type bounds are only allowed in where clauses and function signatures, not in impl header
--> $DIR/bad-universal-in-impl-sig.rs:10:16
|
LL | impl dyn Trait<Item: Trait2> {}
| ^^^^^^^^^^^^
error: aborting due to previous error

View file

@ -3,28 +3,33 @@
use std::mem::ManuallyDrop;
struct S1 { f: dyn Iterator<Item: Copy> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Iterator`) must be specified
struct S2 { f: Box<dyn Iterator<Item: Copy>> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Iterator`) must be specified
struct S3 { f: dyn Iterator<Item: 'static> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Iterator`) must be specified
enum E1 { V(dyn Iterator<Item: Copy>) }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Copy> + 'static)`
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Iterator`) must be specified
enum E2 { V(Box<dyn Iterator<Item: Copy>>) }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Iterator`) must be specified
enum E3 { V(dyn Iterator<Item: 'static>) }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)`
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Iterator`) must be specified
union U1 { f: ManuallyDrop<dyn Iterator<Item: Copy>> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Copy> + 'static)`
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Iterator`) must be specified
union U2 { f: ManuallyDrop<Box<dyn Iterator<Item: Copy>>> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Iterator`) must be specified
union U3 { f: ManuallyDrop<dyn Iterator<Item: 'static>> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)`
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~| ERROR the value of the associated type `Item` (from trait `Iterator`) must be specified
fn main() {}

View file

@ -1,131 +1,129 @@
error: associated type bounds are not allowed within structs, enums, or unions
error: associated type bounds are only allowed in where clauses and function signatures, not in field type
--> $DIR/inside-adt.rs:5:29
|
LL | struct S1 { f: dyn Iterator<Item: Copy> }
| ^^^^^^^^^^
error: associated type bounds are not allowed within structs, enums, or unions
--> $DIR/inside-adt.rs:7:33
error: associated type bounds are only allowed in where clauses and function signatures, not in field type
--> $DIR/inside-adt.rs:8:33
|
LL | struct S2 { f: Box<dyn Iterator<Item: Copy>> }
| ^^^^^^^^^^
error: associated type bounds are not allowed within structs, enums, or unions
--> $DIR/inside-adt.rs:9:29
error: associated type bounds are only allowed in where clauses and function signatures, not in field type
--> $DIR/inside-adt.rs:11:29
|
LL | struct S3 { f: dyn Iterator<Item: 'static> }
| ^^^^^^^^^^^^^
error: associated type bounds are not allowed within structs, enums, or unions
--> $DIR/inside-adt.rs:12:26
error: associated type bounds are only allowed in where clauses and function signatures, not in field type
--> $DIR/inside-adt.rs:15:26
|
LL | enum E1 { V(dyn Iterator<Item: Copy>) }
| ^^^^^^^^^^
error: associated type bounds are not allowed within structs, enums, or unions
--> $DIR/inside-adt.rs:15:30
error: associated type bounds are only allowed in where clauses and function signatures, not in field type
--> $DIR/inside-adt.rs:18:30
|
LL | enum E2 { V(Box<dyn Iterator<Item: Copy>>) }
| ^^^^^^^^^^
error: associated type bounds are not allowed within structs, enums, or unions
--> $DIR/inside-adt.rs:17:26
error: associated type bounds are only allowed in where clauses and function signatures, not in field type
--> $DIR/inside-adt.rs:21:26
|
LL | enum E3 { V(dyn Iterator<Item: 'static>) }
| ^^^^^^^^^^^^^
error: associated type bounds are not allowed within structs, enums, or unions
--> $DIR/inside-adt.rs:21:41
error: associated type bounds are only allowed in where clauses and function signatures, not in field type
--> $DIR/inside-adt.rs:25:41
|
LL | union U1 { f: ManuallyDrop<dyn Iterator<Item: Copy>> }
| ^^^^^^^^^^
error: associated type bounds are not allowed within structs, enums, or unions
--> $DIR/inside-adt.rs:24:45
error: associated type bounds are only allowed in where clauses and function signatures, not in field type
--> $DIR/inside-adt.rs:28:45
|
LL | union U2 { f: ManuallyDrop<Box<dyn Iterator<Item: Copy>>> }
| ^^^^^^^^^^
error: associated type bounds are not allowed within structs, enums, or unions
--> $DIR/inside-adt.rs:26:41
error: associated type bounds are only allowed in where clauses and function signatures, not in field type
--> $DIR/inside-adt.rs:31:41
|
LL | union U3 { f: ManuallyDrop<dyn Iterator<Item: 'static>> }
| ^^^^^^^^^^^^^
error[E0277]: the size for values of type `(dyn Iterator<Item = impl Copy> + 'static)` cannot be known at compilation time
--> $DIR/inside-adt.rs:12:13
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> $DIR/inside-adt.rs:5:20
|
LL | struct S1 { f: dyn Iterator<Item: Copy> }
| ^^^^^^^^^^^^^^^^^^^^
| |
| associated type `Item` must be specified
| help: specify the associated types: `Iterator<Item: Copy, Item = Type>`
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> $DIR/inside-adt.rs:8:24
|
LL | struct S2 { f: Box<dyn Iterator<Item: Copy>> }
| ^^^^^^^^^^^^^^^^^^^^
| |
| associated type `Item` must be specified
| help: specify the associated types: `Iterator<Item: Copy, Item = Type>`
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> $DIR/inside-adt.rs:11:20
|
LL | struct S3 { f: dyn Iterator<Item: 'static> }
| ^^^^^^^^^^^^^^^^^^^^^^^ help: specify the associated type: `Iterator<Item: 'static, Item = Type>`
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> $DIR/inside-adt.rs:15:17
|
LL | enum E1 { V(dyn Iterator<Item: Copy>) }
| ^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Copy> + 'static)`
= note: no field of an enum variant may have a dynamically sized type
= help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
|
LL | enum E1 { V(&dyn Iterator<Item: Copy>) }
| +
help: the `Box` type always has a statically known size and allocates its contents in the heap
|
LL | enum E1 { V(Box<dyn Iterator<Item: Copy>>) }
| ++++ +
| ^^^^^^^^^^^^^^^^^^^^
| |
| associated type `Item` must be specified
| help: specify the associated types: `Iterator<Item: Copy, Item = Type>`
error[E0277]: the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)` cannot be known at compilation time
--> $DIR/inside-adt.rs:17:13
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> $DIR/inside-adt.rs:18:21
|
LL | enum E2 { V(Box<dyn Iterator<Item: Copy>>) }
| ^^^^^^^^^^^^^^^^^^^^
| |
| associated type `Item` must be specified
| help: specify the associated types: `Iterator<Item: Copy, Item = Type>`
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> $DIR/inside-adt.rs:21:17
|
LL | enum E3 { V(dyn Iterator<Item: 'static>) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Sized + 'static> + 'static)`
= note: no field of an enum variant may have a dynamically sized type
= help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
|
LL | enum E3 { V(&dyn Iterator<Item: 'static>) }
| +
help: the `Box` type always has a statically known size and allocates its contents in the heap
|
LL | enum E3 { V(Box<dyn Iterator<Item: 'static>>) }
| ++++ +
| ^^^^^^^^^^^^^^^^^^^^^^^ help: specify the associated type: `Iterator<Item: 'static, Item = Type>`
error[E0277]: the size for values of type `(dyn Iterator<Item = impl Copy> + 'static)` cannot be known at compilation time
--> $DIR/inside-adt.rs:21:15
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> $DIR/inside-adt.rs:25:32
|
LL | union U1 { f: ManuallyDrop<dyn Iterator<Item: Copy>> }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `ManuallyDrop<(dyn Iterator<Item = impl Copy> + 'static)>`, the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Copy> + 'static)`
= note: required because it appears within the type `ManuallyDrop<dyn Iterator<Item = impl Copy>>`
= note: no field of a union may have a dynamically sized type
= help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
|
LL | union U1 { f: &ManuallyDrop<dyn Iterator<Item: Copy>> }
| +
help: the `Box` type always has a statically known size and allocates its contents in the heap
|
LL | union U1 { f: Box<ManuallyDrop<dyn Iterator<Item: Copy>>> }
| ++++ +
| ^^^^^^^^^^^^^^^^^^^^
| |
| associated type `Item` must be specified
| help: specify the associated types: `Iterator<Item: Copy, Item = Type>`
error[E0277]: the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)` cannot be known at compilation time
--> $DIR/inside-adt.rs:26:15
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> $DIR/inside-adt.rs:28:36
|
LL | union U2 { f: ManuallyDrop<Box<dyn Iterator<Item: Copy>>> }
| ^^^^^^^^^^^^^^^^^^^^
| |
| associated type `Item` must be specified
| help: specify the associated types: `Iterator<Item: Copy, Item = Type>`
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> $DIR/inside-adt.rs:31:32
|
LL | union U3 { f: ManuallyDrop<dyn Iterator<Item: 'static>> }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `ManuallyDrop<(dyn Iterator<Item = impl Sized + 'static> + 'static)>`, the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Sized + 'static> + 'static)`
= note: required because it appears within the type `ManuallyDrop<dyn Iterator<Item = impl Sized>>`
= note: no field of a union may have a dynamically sized type
= help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
|
LL | union U3 { f: &ManuallyDrop<dyn Iterator<Item: 'static>> }
| +
help: the `Box` type always has a statically known size and allocates its contents in the heap
|
LL | union U3 { f: Box<ManuallyDrop<dyn Iterator<Item: 'static>>> }
| ++++ +
| ^^^^^^^^^^^^^^^^^^^^^^^ help: specify the associated type: `Iterator<Item: 'static, Item = Type>`
error: aborting due to 13 previous errors
error: aborting due to 18 previous errors
For more information about this error, try `rustc --explain E0277`.
For more information about this error, try `rustc --explain E0191`.