Skip to content

Commit 4f67b06

Browse files
authored
Merge branch 'main' into port/61372
2 parents f6534fe + ed2d8c9 commit 4f67b06

30 files changed

+177
-546
lines changed

internal/checker/checker.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12417,17 +12417,32 @@ func (c *Checker) checkNullishCoalesceOperands(left *ast.Node, right *ast.Node)
1241712417
c.grammarErrorOnNode(right, diagnostics.X_0_and_1_operations_cannot_be_mixed_without_parentheses, scanner.TokenToString(ast.KindQuestionQuestionToken), scanner.TokenToString(operatorToken.Kind))
1241812418
}
1241912419
}
12420+
c.checkNullishCoalesceOperandLeft(left)
12421+
c.checkNullishCoalesceOperandRight(right)
12422+
}
12423+
12424+
func (c *Checker) checkNullishCoalesceOperandLeft(left *ast.Node) {
1242012425
leftTarget := ast.SkipOuterExpressions(left, ast.OEKAll)
1242112426
nullishSemantics := c.getSyntacticNullishnessSemantics(leftTarget)
1242212427
if nullishSemantics != PredicateSemanticsSometimes {
12423-
if left.Parent.Parent.Kind == ast.KindBinaryExpression {
12424-
c.error(leftTarget, diagnostics.This_binary_expression_is_never_nullish_Are_you_missing_parentheses)
12428+
if nullishSemantics == PredicateSemanticsAlways {
12429+
c.error(leftTarget, diagnostics.This_expression_is_always_nullish)
1242512430
} else {
12426-
if nullishSemantics == PredicateSemanticsAlways {
12427-
c.error(leftTarget, diagnostics.This_expression_is_always_nullish)
12428-
} else {
12429-
c.error(leftTarget, diagnostics.Right_operand_of_is_unreachable_because_the_left_operand_is_never_nullish)
12430-
}
12431+
c.error(leftTarget, diagnostics.Right_operand_of_is_unreachable_because_the_left_operand_is_never_nullish)
12432+
}
12433+
}
12434+
}
12435+
12436+
func (c *Checker) checkNullishCoalesceOperandRight(right *ast.Node) {
12437+
binaryExpression := right.Parent
12438+
if binaryExpression.Parent != nil && ast.IsBinaryExpression(binaryExpression.Parent) && binaryExpression.Parent.AsBinaryExpression().OperatorToken.Kind == ast.KindQuestionQuestionToken {
12439+
rightTarget := ast.SkipOuterExpressions(right, ast.OEKAll)
12440+
nullishSemantics := c.getSyntacticNullishnessSemantics(rightTarget)
12441+
switch nullishSemantics {
12442+
case PredicateSemanticsAlways:
12443+
c.error(rightTarget, diagnostics.This_expression_is_always_nullish)
12444+
case PredicateSemanticsNever:
12445+
c.error(rightTarget, diagnostics.This_expression_is_never_nullish)
1243112446
}
1243212447
}
1243312448
}
@@ -15716,14 +15731,14 @@ func (c *Checker) getWriteTypeOfSymbolWithDeferredType(symbol *ast.Symbol) *Type
1571615731
// properties deriving from set accessors will either pre-compute or defer the union or
1571715732
// intersection of the writeTypes of their constituents.
1571815733
func (c *Checker) getWriteTypeOfSymbol(symbol *ast.Symbol) *Type {
15719-
if symbol.Flags&ast.SymbolFlagsProperty != 0 {
15720-
if symbol.CheckFlags&ast.CheckFlagsSyntheticProperty != 0 {
15721-
if symbol.CheckFlags&ast.CheckFlagsDeferredType != 0 {
15722-
return c.getWriteTypeOfSymbolWithDeferredType(symbol)
15723-
}
15724-
links := c.valueSymbolLinks.Get(symbol)
15725-
return core.OrElse(links.writeType, links.resolvedType)
15734+
if symbol.CheckFlags&ast.CheckFlagsSyntheticProperty != 0 {
15735+
if symbol.CheckFlags&ast.CheckFlagsDeferredType != 0 {
15736+
return c.getWriteTypeOfSymbolWithDeferredType(symbol)
1572615737
}
15738+
links := c.valueSymbolLinks.Get(symbol)
15739+
return core.OrElse(links.writeType, links.resolvedType)
15740+
}
15741+
if symbol.Flags&ast.SymbolFlagsProperty != 0 {
1572715742
return c.removeMissingType(c.getTypeOfSymbol(symbol), symbol.Flags&ast.SymbolFlagsOptional != 0)
1572815743
}
1572915744
if symbol.Flags&ast.SymbolFlagsAccessor != 0 {
@@ -20614,6 +20629,7 @@ func (c *Checker) getUnionOrIntersectionProperty(t *Type, name string, skipObjec
2061420629
}
2061520630

2061620631
func (c *Checker) createUnionOrIntersectionProperty(containingType *Type, name string, skipObjectFunctionPropertyAugment bool) *ast.Symbol {
20632+
propFlags := ast.SymbolFlagsNone
2061720633
var singleProp *ast.Symbol
2061820634
var propSet collections.OrderedSet[*ast.Symbol]
2061920635
var indexTypes []*Type
@@ -20643,6 +20659,7 @@ func (c *Checker) createUnionOrIntersectionProperty(containingType *Type, name s
2064320659
}
2064420660
if singleProp == nil {
2064520661
singleProp = prop
20662+
propFlags = core.OrElse(prop.Flags&ast.SymbolFlagsAccessor, ast.SymbolFlagsProperty)
2064620663
} else if prop != singleProp {
2064720664
isInstantiation := c.getTargetSymbol(prop) == c.getTargetSymbol(singleProp)
2064820665
// If the symbols are instances of one another with identical types - consider the symbols
@@ -20659,6 +20676,13 @@ func (c *Checker) createUnionOrIntersectionProperty(containingType *Type, name s
2065920676
}
2066020677
propSet.Add(prop)
2066120678
}
20679+
// classes created by mixins are represented as intersections
20680+
// and overriding a property in a derived class redefines it completely at runtime
20681+
// so a get accessor can't be merged with a set accessor in a base class,
20682+
// for that reason the accessor flags are only used when they are the same in all constituents
20683+
if propFlags&ast.SymbolFlagsAccessor != 0 && (prop.Flags&ast.SymbolFlagsAccessor != (propFlags & ast.SymbolFlagsAccessor)) {
20684+
propFlags = (propFlags &^ ast.SymbolFlagsAccessor) | ast.SymbolFlagsProperty
20685+
}
2066220686
}
2066320687
if isUnion && c.isReadonlySymbol(prop) {
2066420688
checkFlags |= ast.CheckFlagsReadonly
@@ -20686,6 +20710,7 @@ func (c *Checker) createUnionOrIntersectionProperty(containingType *Type, name s
2068620710
indexInfo = c.getApplicableIndexInfoForName(t, name)
2068720711
}
2068820712
if indexInfo != nil {
20713+
propFlags = propFlags&^ast.SymbolFlagsAccessor | ast.SymbolFlagsProperty
2068920714
checkFlags |= ast.CheckFlagsWritePartial | (core.IfElse(indexInfo.isReadonly, ast.CheckFlagsReadonly, 0))
2069020715
if isTupleType(t) {
2069120716
indexType := c.getRestTypeOfTupleType(t)
@@ -20778,7 +20803,7 @@ func (c *Checker) createUnionOrIntersectionProperty(containingType *Type, name s
2077820803
propTypes = append(propTypes, t)
2077920804
}
2078020805
propTypes = append(propTypes, indexTypes...)
20781-
result := c.newSymbolEx(ast.SymbolFlagsProperty|optionalFlag, name, checkFlags|syntheticFlag)
20806+
result := c.newSymbolEx(propFlags|optionalFlag, name, checkFlags|syntheticFlag)
2078220807
result.Declarations = declarations
2078320808
if !hasNonUniformValueDeclaration && firstValueDeclaration != nil {
2078420809
result.ValueDeclaration = firstValueDeclaration

internal/checker/emitresolver.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,14 @@ func (r *emitResolver) hasVisibleDeclarations(symbol *ast.Symbol, shouldComputeA
409409
continue
410410
}
411411
if symbol.Flags&ast.SymbolFlagsBlockScopedVariable != 0 {
412-
variableStatement := ast.FindAncestor(declaration, ast.IsVariableStatement)
412+
rootDeclaration := ast.WalkUpBindingElementsAndPatterns(declaration)
413+
if ast.IsParameter(rootDeclaration) {
414+
return nil
415+
}
416+
variableStatement := rootDeclaration.Parent.Parent
417+
if !ast.IsVariableStatement(variableStatement) {
418+
return nil
419+
}
413420
if ast.HasSyntacticModifier(variableStatement, ast.ModifierFlagsExport) {
414421
continue // no alias to add, already exported
415422
}

internal/checker/nodebuilderimpl.go

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,22 +2214,45 @@ func (b *nodeBuilderImpl) addPropertyToElementList(propertySymbol *ast.Symbol, t
22142214

22152215
if propertySymbol.Flags&ast.SymbolFlagsAccessor != 0 {
22162216
writeType := b.ch.getWriteTypeOfSymbol(propertySymbol)
2217-
if propertyType != writeType && !b.ch.isErrorType(propertyType) && !b.ch.isErrorType(writeType) {
2218-
getterDeclaration := ast.GetDeclarationOfKind(propertySymbol, ast.KindGetAccessor)
2219-
getterSignature := b.ch.getSignatureFromDeclaration(getterDeclaration)
2220-
getter := b.signatureToSignatureDeclarationHelper(getterSignature, ast.KindGetAccessor, &SignatureToSignatureDeclarationOptions{
2221-
name: propertyName,
2222-
})
2223-
b.setCommentRange(getter, getterDeclaration)
2224-
typeElements = append(typeElements, getter)
2225-
setterDeclaration := ast.GetDeclarationOfKind(propertySymbol, ast.KindSetAccessor)
2226-
setterSignature := b.ch.getSignatureFromDeclaration(setterDeclaration)
2227-
setter := b.signatureToSignatureDeclarationHelper(setterSignature, ast.KindSetAccessor, &SignatureToSignatureDeclarationOptions{
2228-
name: propertyName,
2229-
})
2230-
b.setCommentRange(setter, setterDeclaration)
2231-
typeElements = append(typeElements, setter)
2232-
return typeElements
2217+
if !b.ch.isErrorType(propertyType) && !b.ch.isErrorType(writeType) {
2218+
propDeclaration := ast.GetDeclarationOfKind(propertySymbol, ast.KindPropertyDeclaration)
2219+
if propertyType != writeType || propertySymbol.Parent.Flags&ast.SymbolFlagsClass != 0 && propDeclaration == nil {
2220+
if getterDeclaration := ast.GetDeclarationOfKind(propertySymbol, ast.KindGetAccessor); getterDeclaration != nil {
2221+
getterSignature := b.ch.getSignatureFromDeclaration(getterDeclaration)
2222+
getter := b.signatureToSignatureDeclarationHelper(getterSignature, ast.KindGetAccessor, &SignatureToSignatureDeclarationOptions{
2223+
name: propertyName,
2224+
})
2225+
b.setCommentRange(getter, getterDeclaration)
2226+
typeElements = append(typeElements, getter)
2227+
}
2228+
if setterDeclaration := ast.GetDeclarationOfKind(propertySymbol, ast.KindSetAccessor); setterDeclaration != nil {
2229+
setterSignature := b.ch.getSignatureFromDeclaration(setterDeclaration)
2230+
setter := b.signatureToSignatureDeclarationHelper(setterSignature, ast.KindSetAccessor, &SignatureToSignatureDeclarationOptions{
2231+
name: propertyName,
2232+
})
2233+
b.setCommentRange(setter, setterDeclaration)
2234+
typeElements = append(typeElements, setter)
2235+
}
2236+
return typeElements
2237+
} else if propertySymbol.Parent.Flags&ast.SymbolFlagsClass != 0 && propDeclaration != nil && core.Find(propDeclaration.ModifierNodes(), func(m *ast.Node) bool {
2238+
return m.Kind == ast.KindAccessorKeyword
2239+
}) != nil {
2240+
fakeGetterSignature := b.ch.newSignature(SignatureFlagsNone, nil, nil, nil, nil, propertyType, nil, 0)
2241+
fakeGetterDeclaration := b.signatureToSignatureDeclarationHelper(fakeGetterSignature, ast.KindGetAccessor, &SignatureToSignatureDeclarationOptions{
2242+
name: propertyName,
2243+
})
2244+
b.setCommentRange(fakeGetterDeclaration, propDeclaration)
2245+
typeElements = append(typeElements, fakeGetterDeclaration)
2246+
2247+
setterParam := b.ch.newSymbol(ast.SymbolFlagsFunctionScopedVariable, "arg")
2248+
b.ch.valueSymbolLinks.Get(setterParam).resolvedType = writeType
2249+
fakeSetterSignature := b.ch.newSignature(SignatureFlagsNone, nil, nil, nil, []*ast.Symbol{setterParam}, b.ch.voidType, nil, 0)
2250+
fakeSetterDeclaration := b.signatureToSignatureDeclarationHelper(fakeSetterSignature, ast.KindSetAccessor, &SignatureToSignatureDeclarationOptions{
2251+
name: propertyName,
2252+
})
2253+
typeElements = append(typeElements, fakeSetterDeclaration)
2254+
return typeElements
2255+
}
22332256
}
22342257
}
22352258

internal/parser/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4324,6 +4324,7 @@ func (p *Parser) parseArrowFunctionExpressionBody(isAsync bool, allowReturnTypeI
43244324
}
43254325
saveContextFlags := p.contextFlags
43264326
p.setContextFlags(ast.NodeFlagsAwaitContext, isAsync)
4327+
p.setContextFlags(ast.NodeFlagsYieldContext, false)
43274328
node := p.parseAssignmentExpressionOrHigherWorker(allowReturnTypeInArrowFunction)
43284329
p.contextFlags = saveContextFlags
43294330
return node

testdata/baselines/reference/submodule/compiler/declarationEmitGenericTypeParamerSerialization3.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ export const Cls = wrapper("test");
3636
//// [declarationEmitGenericTypeParamerSerialization3.d.ts]
3737
export declare function wrapper<T>(value: T): {
3838
new (): {
39-
name: T;
39+
get name(): T;
40+
set name(arg: T);
4041
};
4142
};
4243
export declare const Cls: {
4344
new (): {
44-
name: string;
45+
get name(): string;
46+
set name(arg: string);
4547
};
4648
};

testdata/baselines/reference/submodule/compiler/declarationEmitGenericTypeParamerSerialization3.js.diff

Lines changed: 0 additions & 18 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/predicateSemantics.errors.txt

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@ predicateSemantics.ts(28,13): error TS2871: This expression is always nullish.
66
predicateSemantics.ts(29,13): error TS2871: This expression is always nullish.
77
predicateSemantics.ts(30,13): error TS2872: This kind of expression is always truthy.
88
predicateSemantics.ts(31,13): error TS2872: This kind of expression is always truthy.
9-
predicateSemantics.ts(32,13): error TS2870: This binary expression is never nullish. Are you missing parentheses?
10-
predicateSemantics.ts(33,13): error TS2870: This binary expression is never nullish. Are you missing parentheses?
11-
predicateSemantics.ts(34,13): error TS2870: This binary expression is never nullish. Are you missing parentheses?
9+
predicateSemantics.ts(32,13): error TS2871: This expression is always nullish.
10+
predicateSemantics.ts(32,21): error TS2871: This expression is always nullish.
11+
predicateSemantics.ts(33,13): error TS2871: This expression is always nullish.
12+
predicateSemantics.ts(34,13): error TS2871: This expression is always nullish.
13+
predicateSemantics.ts(34,22): error TS2871: This expression is always nullish.
14+
predicateSemantics.ts(36,20): error TS2871: This expression is always nullish.
15+
predicateSemantics.ts(37,20): error TS2871: This expression is always nullish.
1216
predicateSemantics.ts(38,21): error TS2871: This expression is always nullish.
1317
predicateSemantics.ts(39,21): error TS2871: This expression is always nullish.
14-
predicateSemantics.ts(40,21): error TS2870: This binary expression is never nullish. Are you missing parentheses?
15-
predicateSemantics.ts(45,13): error TS2870: This binary expression is never nullish. Are you missing parentheses?
16-
predicateSemantics.ts(46,13): error TS2870: This binary expression is never nullish. Are you missing parentheses?
17-
predicateSemantics.ts(47,13): error TS2870: This binary expression is never nullish. Are you missing parentheses?
18+
predicateSemantics.ts(40,21): error TS2871: This expression is always nullish.
19+
predicateSemantics.ts(40,29): error TS2871: This expression is always nullish.
20+
predicateSemantics.ts(41,21): error TS2871: This expression is always nullish.
21+
predicateSemantics.ts(42,20): error TS2881: This expression is never nullish.
22+
predicateSemantics.ts(43,21): error TS2881: This expression is never nullish.
23+
predicateSemantics.ts(45,13): error TS2871: This expression is always nullish.
24+
predicateSemantics.ts(45,21): error TS2871: This expression is always nullish.
25+
predicateSemantics.ts(45,29): error TS2871: This expression is always nullish.
26+
predicateSemantics.ts(46,13): error TS2871: This expression is always nullish.
27+
predicateSemantics.ts(46,21): error TS2881: This expression is never nullish.
28+
predicateSemantics.ts(47,13): error TS2871: This expression is always nullish.
29+
predicateSemantics.ts(47,22): error TS2881: This expression is never nullish.
1830
predicateSemantics.ts(50,8): error TS2872: This kind of expression is always truthy.
1931
predicateSemantics.ts(51,11): error TS2872: This kind of expression is always truthy.
2032
predicateSemantics.ts(52,8): error TS2872: This kind of expression is always truthy.
@@ -26,7 +38,7 @@ predicateSemantics.ts(89,1): error TS2869: Right operand of ?? is unreachable be
2638
predicateSemantics.ts(90,1): error TS2869: Right operand of ?? is unreachable because the left operand is never nullish.
2739

2840

29-
==== predicateSemantics.ts (26 errors) ====
41+
==== predicateSemantics.ts (38 errors) ====
3042
declare let opt: number | undefined;
3143

3244
// OK: One or other operand is possibly nullish
@@ -76,16 +88,24 @@ predicateSemantics.ts(90,1): error TS2869: Right operand of ?? is unreachable be
7688
!!! error TS2872: This kind of expression is always truthy.
7789
const p07 = null ?? null ?? null;
7890
~~~~
79-
!!! error TS2870: This binary expression is never nullish. Are you missing parentheses?
91+
!!! error TS2871: This expression is always nullish.
92+
~~~~
93+
!!! error TS2871: This expression is always nullish.
8094
const p08 = null ?? opt ?? null;
8195
~~~~
82-
!!! error TS2870: This binary expression is never nullish. Are you missing parentheses?
96+
!!! error TS2871: This expression is always nullish.
8397
const p09 = null ?? (opt ? null : undefined) ?? null;
8498
~~~~
85-
!!! error TS2870: This binary expression is never nullish. Are you missing parentheses?
99+
!!! error TS2871: This expression is always nullish.
100+
~~~~~~~~~~~~~~~~~~~~~~
101+
!!! error TS2871: This expression is always nullish.
86102

87103
const p10 = opt ?? null ?? 1;
104+
~~~~
105+
!!! error TS2871: This expression is always nullish.
88106
const p11 = opt ?? null ?? null;
107+
~~~~
108+
!!! error TS2871: This expression is always nullish.
89109
const p12 = opt ?? (null ?? 1);
90110
~~~~
91111
!!! error TS2871: This expression is always nullish.
@@ -94,20 +114,36 @@ predicateSemantics.ts(90,1): error TS2869: Right operand of ?? is unreachable be
94114
!!! error TS2871: This expression is always nullish.
95115
const p14 = opt ?? (null ?? null ?? null);
96116
~~~~
97-
!!! error TS2870: This binary expression is never nullish. Are you missing parentheses?
117+
!!! error TS2871: This expression is always nullish.
118+
~~~~
119+
!!! error TS2871: This expression is always nullish.
98120
const p15 = opt ?? (opt ? null : undefined) ?? null;
121+
~~~~~~~~~~~~~~~~~~~~~~
122+
!!! error TS2871: This expression is always nullish.
99123
const p16 = opt ?? 1 ?? 2;
124+
~
125+
!!! error TS2881: This expression is never nullish.
100126
const p17 = opt ?? (opt ? 1 : 2) ?? 3;
127+
~~~~~~~~~~~
128+
!!! error TS2881: This expression is never nullish.
101129

102130
const p21 = null ?? null ?? null ?? null;
103131
~~~~
104-
!!! error TS2870: This binary expression is never nullish. Are you missing parentheses?
132+
!!! error TS2871: This expression is always nullish.
133+
~~~~
134+
!!! error TS2871: This expression is always nullish.
135+
~~~~
136+
!!! error TS2871: This expression is always nullish.
105137
const p22 = null ?? 1 ?? 1;
106138
~~~~
107-
!!! error TS2870: This binary expression is never nullish. Are you missing parentheses?
139+
!!! error TS2871: This expression is always nullish.
140+
~
141+
!!! error TS2881: This expression is never nullish.
108142
const p23 = null ?? (opt ? 1 : 2) ?? 1;
109143
~~~~
110-
!!! error TS2870: This binary expression is never nullish. Are you missing parentheses?
144+
!!! error TS2871: This expression is always nullish.
145+
~~~~~~~~~~~
146+
!!! error TS2881: This expression is never nullish.
111147

112148
// Outer expression tests
113149
while ({} as any) { }

0 commit comments

Comments
 (0)