Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Allow variance annotations on generic references (#56418)" #59793

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41125,7 +41125,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const modifiers = getTypeParameterModifiers(typeParameter) & (ModifierFlags.In | ModifierFlags.Out);
if (modifiers) {
const symbol = getSymbolOfDeclaration(node.parent);
if (isTypeAliasDeclaration(node.parent) && !(getObjectFlags(getDeclaredTypeOfSymbol(symbol)) & (ObjectFlags.Reference | ObjectFlags.Anonymous | ObjectFlags.Mapped))) {
if (isTypeAliasDeclaration(node.parent) && !(getObjectFlags(getDeclaredTypeOfSymbol(symbol)) & (ObjectFlags.Anonymous | ObjectFlags.Mapped))) {
error(node, Diagnostics.Variance_annotations_are_only_supported_in_type_aliases_for_object_function_constructor_and_mapped_types);
}
else if (modifiers === ModifierFlags.In || modifiers === ModifierFlags.Out) {
Expand Down
71 changes: 68 additions & 3 deletions tests/baselines/reference/varianceReferences.errors.txt
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new assignability errors (for newly added assignments) are here the same as on main - that's why those variance annotations that were allowed shouldn't be allowed (because they are completely ignored anyway)

Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
varianceReferences.ts(3,32): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
varianceReferences.ts(8,28): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
varianceReferences.ts(14,32): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
varianceReferences.ts(9,1): error TS2322: Type '1 | 2' is not assignable to type '1'.
Type '2' is not assignable to type '1'.
varianceReferences.ts(14,28): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
varianceReferences.ts(19,1): error TS2322: Type '1 | 2' is not assignable to type '1'.
Type '2' is not assignable to type '1'.
varianceReferences.ts(26,32): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
varianceReferences.ts(31,1): error TS2322: Type '1 | 2' is not assignable to type '1'.
Type '2' is not assignable to type '1'.
varianceReferences.ts(38,20): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
varianceReferences.ts(43,1): error TS2322: Type 'VarianceShape<1 | 2>' is not assignable to type 'VarianceShape<1>'.
Type '1 | 2' is not assignable to type '1'.
Type '2' is not assignable to type '1'.
varianceReferences.ts(53,24): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
varianceReferences.ts(58,1): error TS2322: Type 'VarianceDeepShape<1 | 2>' is not assignable to type 'VarianceDeepShape<1>'.
Type '1 | 2' is not assignable to type '1'.
Type '2' is not assignable to type '1'.


==== varianceReferences.ts (3 errors) ====
==== varianceReferences.ts (10 errors) ====
type NumericConstraint<Value extends number> = Value;

type VarianceConstrainedNumber<in out Value extends number> =
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
NumericConstraint<Value>;

declare let vcn1: VarianceConstrainedNumber<1>;
declare let vcn12: VarianceConstrainedNumber<1 | 2>;

vcn1 = vcn12;
~~~~
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
!!! error TS2322: Type '2' is not assignable to type '1'.
vcn12 = vcn1;

type Unconstrained<Value> = Value;

type VarianceUnconstrained<in out Value> = Unconstrained<Value>;
~~~~~~~~~~~~
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.

declare let vu1: VarianceUnconstrained<1>;
declare let vu12: VarianceUnconstrained<1 | 2>;

vu1 = vu12;
~~~
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
!!! error TS2322: Type '2' is not assignable to type '1'.
vu12 = vu1;

type Level3of3Unconstrained<Value> = Value;
type Level2of3Unconstrained<Value> = Level3of3Unconstrained<Value>;
type Level1of3Unconstrained<Value> = Level2of3Unconstrained<Value>;
Expand All @@ -25,11 +57,32 @@ varianceReferences.ts(14,32): error TS2637: Variance annotations are only suppor
~~~~~~~~~~~~
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.

declare let vdu1: VarianceDeepUnconstrained<1>;
declare let vdu12: VarianceDeepUnconstrained<1 | 2>;

vdu1 = vdu12;
~~~~
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
!!! error TS2322: Type '2' is not assignable to type '1'.
vdu12 = vdu1;

interface Shape<Value> {
value: Value;
}

type VarianceShape<in out Value> = Shape<Value>;
~~~~~~~~~~~~
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.

declare let vs1: VarianceShape<1>;
declare let vs12: VarianceShape<1 | 2>;

vs1 = vs12;
~~~
!!! error TS2322: Type 'VarianceShape<1 | 2>' is not assignable to type 'VarianceShape<1>'.
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
!!! error TS2322: Type '2' is not assignable to type '1'.
vs12 = vs1;

interface Level3of3Shape<Value> {
value: Value;
Expand All @@ -39,4 +92,16 @@ varianceReferences.ts(14,32): error TS2637: Variance annotations are only suppor
type Level1of3Shape<Value> = Level2of3Shape<Value>;

type VarianceDeepShape<in out Value> = Level1of3Shape<Value>;
~~~~~~~~~~~~
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.

declare let vds1: VarianceDeepShape<1>;
declare let vds12: VarianceDeepShape<1 | 2>;

vds1 = vds12;
~~~~
!!! error TS2322: Type 'VarianceDeepShape<1 | 2>' is not assignable to type 'VarianceDeepShape<1>'.
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
!!! error TS2322: Type '2' is not assignable to type '1'.
vds12 = vds1;

35 changes: 0 additions & 35 deletions tests/baselines/reference/varianceReferences.js

This file was deleted.

172 changes: 126 additions & 46 deletions tests/baselines/reference/varianceReferences.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -14,79 +14,159 @@ type VarianceConstrainedNumber<in out Value extends number> =
>NumericConstraint : Symbol(NumericConstraint, Decl(varianceReferences.ts, 0, 0))
>Value : Symbol(Value, Decl(varianceReferences.ts, 2, 31))

declare let vcn1: VarianceConstrainedNumber<1>;
>vcn1 : Symbol(vcn1, Decl(varianceReferences.ts, 5, 11))
>VarianceConstrainedNumber : Symbol(VarianceConstrainedNumber, Decl(varianceReferences.ts, 0, 53))

declare let vcn12: VarianceConstrainedNumber<1 | 2>;
>vcn12 : Symbol(vcn12, Decl(varianceReferences.ts, 6, 11))
>VarianceConstrainedNumber : Symbol(VarianceConstrainedNumber, Decl(varianceReferences.ts, 0, 53))

vcn1 = vcn12;
>vcn1 : Symbol(vcn1, Decl(varianceReferences.ts, 5, 11))
>vcn12 : Symbol(vcn12, Decl(varianceReferences.ts, 6, 11))

vcn12 = vcn1;
>vcn12 : Symbol(vcn12, Decl(varianceReferences.ts, 6, 11))
>vcn1 : Symbol(vcn1, Decl(varianceReferences.ts, 5, 11))

type Unconstrained<Value> = Value;
>Unconstrained : Symbol(Unconstrained, Decl(varianceReferences.ts, 3, 27))
>Value : Symbol(Value, Decl(varianceReferences.ts, 5, 19))
>Value : Symbol(Value, Decl(varianceReferences.ts, 5, 19))
>Unconstrained : Symbol(Unconstrained, Decl(varianceReferences.ts, 9, 13))
>Value : Symbol(Value, Decl(varianceReferences.ts, 11, 19))
>Value : Symbol(Value, Decl(varianceReferences.ts, 11, 19))

type VarianceUnconstrained<in out Value> = Unconstrained<Value>;
>VarianceUnconstrained : Symbol(VarianceUnconstrained, Decl(varianceReferences.ts, 5, 34))
>Value : Symbol(Value, Decl(varianceReferences.ts, 7, 27))
>Unconstrained : Symbol(Unconstrained, Decl(varianceReferences.ts, 3, 27))
>Value : Symbol(Value, Decl(varianceReferences.ts, 7, 27))
>VarianceUnconstrained : Symbol(VarianceUnconstrained, Decl(varianceReferences.ts, 11, 34))
>Value : Symbol(Value, Decl(varianceReferences.ts, 13, 27))
>Unconstrained : Symbol(Unconstrained, Decl(varianceReferences.ts, 9, 13))
>Value : Symbol(Value, Decl(varianceReferences.ts, 13, 27))

declare let vu1: VarianceUnconstrained<1>;
>vu1 : Symbol(vu1, Decl(varianceReferences.ts, 15, 11))
>VarianceUnconstrained : Symbol(VarianceUnconstrained, Decl(varianceReferences.ts, 11, 34))

declare let vu12: VarianceUnconstrained<1 | 2>;
>vu12 : Symbol(vu12, Decl(varianceReferences.ts, 16, 11))
>VarianceUnconstrained : Symbol(VarianceUnconstrained, Decl(varianceReferences.ts, 11, 34))

vu1 = vu12;
>vu1 : Symbol(vu1, Decl(varianceReferences.ts, 15, 11))
>vu12 : Symbol(vu12, Decl(varianceReferences.ts, 16, 11))

vu12 = vu1;
>vu12 : Symbol(vu12, Decl(varianceReferences.ts, 16, 11))
>vu1 : Symbol(vu1, Decl(varianceReferences.ts, 15, 11))

type Level3of3Unconstrained<Value> = Value;
>Level3of3Unconstrained : Symbol(Level3of3Unconstrained, Decl(varianceReferences.ts, 7, 64))
>Value : Symbol(Value, Decl(varianceReferences.ts, 9, 28))
>Value : Symbol(Value, Decl(varianceReferences.ts, 9, 28))
>Level3of3Unconstrained : Symbol(Level3of3Unconstrained, Decl(varianceReferences.ts, 19, 11))
>Value : Symbol(Value, Decl(varianceReferences.ts, 21, 28))
>Value : Symbol(Value, Decl(varianceReferences.ts, 21, 28))

type Level2of3Unconstrained<Value> = Level3of3Unconstrained<Value>;
>Level2of3Unconstrained : Symbol(Level2of3Unconstrained, Decl(varianceReferences.ts, 9, 43))
>Value : Symbol(Value, Decl(varianceReferences.ts, 10, 28))
>Level3of3Unconstrained : Symbol(Level3of3Unconstrained, Decl(varianceReferences.ts, 7, 64))
>Value : Symbol(Value, Decl(varianceReferences.ts, 10, 28))
>Level2of3Unconstrained : Symbol(Level2of3Unconstrained, Decl(varianceReferences.ts, 21, 43))
>Value : Symbol(Value, Decl(varianceReferences.ts, 22, 28))
>Level3of3Unconstrained : Symbol(Level3of3Unconstrained, Decl(varianceReferences.ts, 19, 11))
>Value : Symbol(Value, Decl(varianceReferences.ts, 22, 28))

type Level1of3Unconstrained<Value> = Level2of3Unconstrained<Value>;
>Level1of3Unconstrained : Symbol(Level1of3Unconstrained, Decl(varianceReferences.ts, 10, 67))
>Value : Symbol(Value, Decl(varianceReferences.ts, 11, 28))
>Level2of3Unconstrained : Symbol(Level2of3Unconstrained, Decl(varianceReferences.ts, 9, 43))
>Value : Symbol(Value, Decl(varianceReferences.ts, 11, 28))
>Level1of3Unconstrained : Symbol(Level1of3Unconstrained, Decl(varianceReferences.ts, 22, 67))
>Value : Symbol(Value, Decl(varianceReferences.ts, 23, 28))
>Level2of3Unconstrained : Symbol(Level2of3Unconstrained, Decl(varianceReferences.ts, 21, 43))
>Value : Symbol(Value, Decl(varianceReferences.ts, 23, 28))

type VarianceDeepUnconstrained<in out Value> = Level1of3Unconstrained<Value>;
>VarianceDeepUnconstrained : Symbol(VarianceDeepUnconstrained, Decl(varianceReferences.ts, 11, 67))
>Value : Symbol(Value, Decl(varianceReferences.ts, 13, 31))
>Level1of3Unconstrained : Symbol(Level1of3Unconstrained, Decl(varianceReferences.ts, 10, 67))
>Value : Symbol(Value, Decl(varianceReferences.ts, 13, 31))
>VarianceDeepUnconstrained : Symbol(VarianceDeepUnconstrained, Decl(varianceReferences.ts, 23, 67))
>Value : Symbol(Value, Decl(varianceReferences.ts, 25, 31))
>Level1of3Unconstrained : Symbol(Level1of3Unconstrained, Decl(varianceReferences.ts, 22, 67))
>Value : Symbol(Value, Decl(varianceReferences.ts, 25, 31))

declare let vdu1: VarianceDeepUnconstrained<1>;
>vdu1 : Symbol(vdu1, Decl(varianceReferences.ts, 27, 11))
>VarianceDeepUnconstrained : Symbol(VarianceDeepUnconstrained, Decl(varianceReferences.ts, 23, 67))

declare let vdu12: VarianceDeepUnconstrained<1 | 2>;
>vdu12 : Symbol(vdu12, Decl(varianceReferences.ts, 28, 11))
>VarianceDeepUnconstrained : Symbol(VarianceDeepUnconstrained, Decl(varianceReferences.ts, 23, 67))

vdu1 = vdu12;
>vdu1 : Symbol(vdu1, Decl(varianceReferences.ts, 27, 11))
>vdu12 : Symbol(vdu12, Decl(varianceReferences.ts, 28, 11))

vdu12 = vdu1;
>vdu12 : Symbol(vdu12, Decl(varianceReferences.ts, 28, 11))
>vdu1 : Symbol(vdu1, Decl(varianceReferences.ts, 27, 11))

interface Shape<Value> {
>Shape : Symbol(Shape, Decl(varianceReferences.ts, 13, 77))
>Value : Symbol(Value, Decl(varianceReferences.ts, 15, 16))
>Shape : Symbol(Shape, Decl(varianceReferences.ts, 31, 13))
>Value : Symbol(Value, Decl(varianceReferences.ts, 33, 16))

value: Value;
>value : Symbol(Shape.value, Decl(varianceReferences.ts, 15, 24))
>Value : Symbol(Value, Decl(varianceReferences.ts, 15, 16))
>value : Symbol(Shape.value, Decl(varianceReferences.ts, 33, 24))
>Value : Symbol(Value, Decl(varianceReferences.ts, 33, 16))
}

type VarianceShape<in out Value> = Shape<Value>;
>VarianceShape : Symbol(VarianceShape, Decl(varianceReferences.ts, 17, 1))
>Value : Symbol(Value, Decl(varianceReferences.ts, 19, 19))
>Shape : Symbol(Shape, Decl(varianceReferences.ts, 13, 77))
>Value : Symbol(Value, Decl(varianceReferences.ts, 19, 19))
>VarianceShape : Symbol(VarianceShape, Decl(varianceReferences.ts, 35, 1))
>Value : Symbol(Value, Decl(varianceReferences.ts, 37, 19))
>Shape : Symbol(Shape, Decl(varianceReferences.ts, 31, 13))
>Value : Symbol(Value, Decl(varianceReferences.ts, 37, 19))

declare let vs1: VarianceShape<1>;
>vs1 : Symbol(vs1, Decl(varianceReferences.ts, 39, 11))
>VarianceShape : Symbol(VarianceShape, Decl(varianceReferences.ts, 35, 1))

declare let vs12: VarianceShape<1 | 2>;
>vs12 : Symbol(vs12, Decl(varianceReferences.ts, 40, 11))
>VarianceShape : Symbol(VarianceShape, Decl(varianceReferences.ts, 35, 1))

vs1 = vs12;
>vs1 : Symbol(vs1, Decl(varianceReferences.ts, 39, 11))
>vs12 : Symbol(vs12, Decl(varianceReferences.ts, 40, 11))

vs12 = vs1;
>vs12 : Symbol(vs12, Decl(varianceReferences.ts, 40, 11))
>vs1 : Symbol(vs1, Decl(varianceReferences.ts, 39, 11))

interface Level3of3Shape<Value> {
>Level3of3Shape : Symbol(Level3of3Shape, Decl(varianceReferences.ts, 19, 48))
>Value : Symbol(Value, Decl(varianceReferences.ts, 21, 25))
>Level3of3Shape : Symbol(Level3of3Shape, Decl(varianceReferences.ts, 43, 11))
>Value : Symbol(Value, Decl(varianceReferences.ts, 45, 25))

value: Value;
>value : Symbol(Level3of3Shape.value, Decl(varianceReferences.ts, 21, 33))
>Value : Symbol(Value, Decl(varianceReferences.ts, 21, 25))
>value : Symbol(Level3of3Shape.value, Decl(varianceReferences.ts, 45, 33))
>Value : Symbol(Value, Decl(varianceReferences.ts, 45, 25))
}

type Level2of3Shape<Value> = Level3of3Shape<Value>;
>Level2of3Shape : Symbol(Level2of3Shape, Decl(varianceReferences.ts, 23, 1))
>Value : Symbol(Value, Decl(varianceReferences.ts, 25, 20))
>Level3of3Shape : Symbol(Level3of3Shape, Decl(varianceReferences.ts, 19, 48))
>Value : Symbol(Value, Decl(varianceReferences.ts, 25, 20))
>Level2of3Shape : Symbol(Level2of3Shape, Decl(varianceReferences.ts, 47, 1))
>Value : Symbol(Value, Decl(varianceReferences.ts, 49, 20))
>Level3of3Shape : Symbol(Level3of3Shape, Decl(varianceReferences.ts, 43, 11))
>Value : Symbol(Value, Decl(varianceReferences.ts, 49, 20))

type Level1of3Shape<Value> = Level2of3Shape<Value>;
>Level1of3Shape : Symbol(Level1of3Shape, Decl(varianceReferences.ts, 25, 51))
>Value : Symbol(Value, Decl(varianceReferences.ts, 26, 20))
>Level2of3Shape : Symbol(Level2of3Shape, Decl(varianceReferences.ts, 23, 1))
>Value : Symbol(Value, Decl(varianceReferences.ts, 26, 20))
>Level1of3Shape : Symbol(Level1of3Shape, Decl(varianceReferences.ts, 49, 51))
>Value : Symbol(Value, Decl(varianceReferences.ts, 50, 20))
>Level2of3Shape : Symbol(Level2of3Shape, Decl(varianceReferences.ts, 47, 1))
>Value : Symbol(Value, Decl(varianceReferences.ts, 50, 20))

type VarianceDeepShape<in out Value> = Level1of3Shape<Value>;
>VarianceDeepShape : Symbol(VarianceDeepShape, Decl(varianceReferences.ts, 26, 51))
>Value : Symbol(Value, Decl(varianceReferences.ts, 28, 23))
>Level1of3Shape : Symbol(Level1of3Shape, Decl(varianceReferences.ts, 25, 51))
>Value : Symbol(Value, Decl(varianceReferences.ts, 28, 23))
>VarianceDeepShape : Symbol(VarianceDeepShape, Decl(varianceReferences.ts, 50, 51))
>Value : Symbol(Value, Decl(varianceReferences.ts, 52, 23))
>Level1of3Shape : Symbol(Level1of3Shape, Decl(varianceReferences.ts, 49, 51))
>Value : Symbol(Value, Decl(varianceReferences.ts, 52, 23))

declare let vds1: VarianceDeepShape<1>;
>vds1 : Symbol(vds1, Decl(varianceReferences.ts, 54, 11))
>VarianceDeepShape : Symbol(VarianceDeepShape, Decl(varianceReferences.ts, 50, 51))

declare let vds12: VarianceDeepShape<1 | 2>;
>vds12 : Symbol(vds12, Decl(varianceReferences.ts, 55, 11))
>VarianceDeepShape : Symbol(VarianceDeepShape, Decl(varianceReferences.ts, 50, 51))

vds1 = vds12;
>vds1 : Symbol(vds1, Decl(varianceReferences.ts, 54, 11))
>vds12 : Symbol(vds12, Decl(varianceReferences.ts, 55, 11))

vds12 = vds1;
>vds12 : Symbol(vds12, Decl(varianceReferences.ts, 55, 11))
>vds1 : Symbol(vds1, Decl(varianceReferences.ts, 54, 11))

Loading