Skip to content
Merged
Prev Previous commit
Next Next commit
Report variance markers as 'sub-XXX' and 'super-XXX'
  • Loading branch information
ahejlsberg committed Mar 12, 2022
commit 05ad13a19539d28587f4d37f00edc57158c0b7fc
15 changes: 11 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ namespace ts {
let instantiationDepth = 0;
let inlineLevel = 0;
let currentNode: Node | undefined;
let varianceTypeParameter: TypeParameter | undefined;

const emptySymbols = createSymbolTable();
const arrayVariances = [VarianceFlags.Covariant];
Expand Down Expand Up @@ -4986,9 +4987,12 @@ namespace ts {
return factory.createTypeReferenceNode(factory.createIdentifier(idText(name)), /*typeArguments*/ undefined);
}
// Ignore constraint/default when creating a usage (as opposed to declaration) of a type parameter.
return type.symbol
? symbolToTypeNode(type.symbol, context, SymbolFlags.Type)
: factory.createTypeReferenceNode(factory.createIdentifier(type === markerSuperType ? "Super" : type === markerSubType ? "Sub" : "?"), /*typeArguments*/ undefined);
if (type.symbol) {
return symbolToTypeNode(type.symbol, context, SymbolFlags.Type);
}
const name = (type === markerSuperType || type === markerSubType) && varianceTypeParameter && varianceTypeParameter.symbol ?
(type === markerSubType ? "sub-" : "super-") + symbolName(varianceTypeParameter.symbol) : "?";
return factory.createTypeReferenceNode(factory.createIdentifier(name), /*typeArguments*/ undefined);
}
if (type.flags & TypeFlags.Union && (type as UnionType).origin) {
type = (type as UnionType).origin!;
Expand Down Expand Up @@ -18391,7 +18395,7 @@ namespace ts {
generalizedSourceType = getTypeNameForErrorDisplay(generalizedSource);
}

if (target.flags & TypeFlags.TypeParameter) {
if (target.flags & TypeFlags.TypeParameter && target !== markerSuperType && target !== markerSubType) {
const constraint = getBaseConstraintOfType(target);
let needsOriginalSource;
if (constraint && (isTypeAssignableTo(generalizedSource, constraint) || (needsOriginalSource = isTypeAssignableTo(source, constraint)))) {
Expand Down Expand Up @@ -34682,7 +34686,10 @@ namespace ts {
const symbol = getSymbolOfNode(node.parent);
const source = createMarkerType(symbol, typeParameter, modifiers === ModifierFlags.Out ? markerSubType : markerSuperType);
const target = createMarkerType(symbol, typeParameter, modifiers === ModifierFlags.Out ? markerSuperType : markerSubType);
const saveVarianceTypeParameter = typeParameter;
varianceTypeParameter = typeParameter;
checkTypeAssignableTo(source, target, node, Diagnostics.Type_0_is_not_assignable_to_type_1_as_implied_by_variance_annotation);
varianceTypeParameter = saveVarianceTypeParameter;
}
}
if (produceDiagnostics) {
Expand Down