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

Local types #3266

Merged
merged 18 commits into from
May 31, 2015
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
8 changes: 4 additions & 4 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,17 +519,17 @@ module ts {
bindBlockScopedDeclaration(<Declaration>node, SymbolFlags.Class, SymbolFlags.ClassExcludes);
break;
case SyntaxKind.InterfaceDeclaration:
bindDeclaration(<Declaration>node, SymbolFlags.Interface, SymbolFlags.InterfaceExcludes, /*isBlockScopeContainer*/ false);
bindBlockScopedDeclaration(<Declaration>node, SymbolFlags.Interface, SymbolFlags.InterfaceExcludes);
break;
case SyntaxKind.TypeAliasDeclaration:
bindDeclaration(<Declaration>node, SymbolFlags.TypeAlias, SymbolFlags.TypeAliasExcludes, /*isBlockScopeContainer*/ false);
bindBlockScopedDeclaration(<Declaration>node, SymbolFlags.TypeAlias, SymbolFlags.TypeAliasExcludes);
break;
case SyntaxKind.EnumDeclaration:
if (isConst(node)) {
bindDeclaration(<Declaration>node, SymbolFlags.ConstEnum, SymbolFlags.ConstEnumExcludes, /*isBlockScopeContainer*/ false);
bindBlockScopedDeclaration(<Declaration>node, SymbolFlags.ConstEnum, SymbolFlags.ConstEnumExcludes);
}
else {
bindDeclaration(<Declaration>node, SymbolFlags.RegularEnum, SymbolFlags.RegularEnumExcludes, /*isBlockScopeContainer*/ false);
bindBlockScopedDeclaration(<Declaration>node, SymbolFlags.RegularEnum, SymbolFlags.RegularEnumExcludes);
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess classes are already block scoped?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct.

}
break;
case SyntaxKind.ModuleDeclaration:
Expand Down
197 changes: 151 additions & 46 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ module ts {
}
}

export function rangeEquals<T>(array1: T[], array2: T[], pos: number, end: number) {
while (pos < end) {
if (array1[pos] !== array2[pos]) {
return false;
}
pos++;
}
return true;
}

/**
* Returns the last element of an array if non-empty, undefined otherwise.
*/
Expand Down
1 change: 0 additions & 1 deletion src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,5 @@ module ts {
decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." },
Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." },
class_expressions_are_not_currently_supported: { code: 9003, category: DiagnosticCategory.Error, key: "'class' expressions are not currently supported." },
class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration: { code: 9004, category: DiagnosticCategory.Error, key: "'class' declarations are only supported directly inside a module or as a top level declaration." },
};
}
4 changes: 0 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2185,9 +2185,5 @@
"'class' expressions are not currently supported.": {
"category": "Error",
"code": 9003
},
"'class' declarations are only supported directly inside a module or as a top level declaration.": {
"category": "Error",
"code": 9004
}
}
527 changes: 229 additions & 298 deletions src/compiler/parser.ts

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ module ts {
_classElementBrand: any;
}

export interface InterfaceDeclaration extends Declaration, ModuleElement {
export interface InterfaceDeclaration extends Declaration, Statement {
name: Identifier;
typeParameters?: NodeArray<TypeParameterDeclaration>;
heritageClauses?: NodeArray<HeritageClause>;
Expand All @@ -929,7 +929,7 @@ module ts {
types?: NodeArray<ExpressionWithTypeArguments>;
}

export interface TypeAliasDeclaration extends Declaration, ModuleElement {
export interface TypeAliasDeclaration extends Declaration, Statement {
name: Identifier;
type: TypeNode;
}
Expand All @@ -941,7 +941,7 @@ module ts {
initializer?: Expression;
}

export interface EnumDeclaration extends Declaration, ModuleElement {
export interface EnumDeclaration extends Declaration, Statement {
name: Identifier;
members: NodeArray<EnumMember>;
}
Expand Down Expand Up @@ -1626,6 +1626,8 @@ module ts {
// Class and interface types (TypeFlags.Class and TypeFlags.Interface)
export interface InterfaceType extends ObjectType {
typeParameters: TypeParameter[]; // Type parameters (undefined if non-generic)
outerTypeParameters: TypeParameter[]; // Outer type parameters (undefined if none)
localTypeParameters: TypeParameter[]; // Local type parameters (undefined if none)
}

export interface InterfaceTypeWithBaseTypes extends InterfaceType {
Copy link
Contributor

Choose a reason for hiding this comment

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

I expected to see things like changes to ClassDeclation/EnumDeclaration/etc. to make them statements.

If they're not statements, then we need to change things like Block to have an array of ModuleElements instead.

Right now the type system isn't correct vis-a-vis what we say you may encounter and what you can actually encounter.

Copy link
Member Author

Choose a reason for hiding this comment

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

They are. You're looking at the type interfaces here, not the node interfaces.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit confused. For example, I don't see a change to EnumDeclaration to make it a Statement. As such, i would not expect that it could be contained within a Block.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, it looks like only ClassDeclaration was changed. I will fix the others.

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. I do not see ClassDeclaration changed here, but you said you did change that one to be a statement, right?
  2. Are there still things that are only ModuleElements but not statements? Like module/namespace declarations for instance? Are there any others?

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. ClassDeclaration definitely has been changed to extend Statement. Not sure what you looking at.
  2. Module, namespace, export, and import declarations are not statements (but export can be used as a modifier and is then allowed).

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. I meant in this PR, but I guess it's been changed before.
  2. To clarify, the export modifier in a statement context is not allowed, it's just parsed as a statement.

Copy link
Member Author

Choose a reason for hiding this comment

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

We always parse all modifiers on a declaration and then issue errors later during type check. We need to do this so our incremental parsing always parses declarations the same. Also, it provides better error handling.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that makes sense. Thanks for explaining.

Expand Down
13 changes: 0 additions & 13 deletions tests/baselines/reference/classDeclarationBlockScoping1.errors.txt

This file was deleted.

10 changes: 10 additions & 0 deletions tests/baselines/reference/classDeclarationBlockScoping1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/classDeclarationBlockScoping1.ts ===
class C {
>C : Symbol(C, Decl(classDeclarationBlockScoping1.ts, 0, 0))
}

{
class C {
>C : Symbol(C, Decl(classDeclarationBlockScoping1.ts, 3, 1))
}
}
10 changes: 10 additions & 0 deletions tests/baselines/reference/classDeclarationBlockScoping1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/classDeclarationBlockScoping1.ts ===
class C {
>C : C
}

{
class C {
>C : C
}
}
18 changes: 0 additions & 18 deletions tests/baselines/reference/classDeclarationBlockScoping2.errors.txt

This file was deleted.

22 changes: 22 additions & 0 deletions tests/baselines/reference/classDeclarationBlockScoping2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/classDeclarationBlockScoping2.ts ===
function f() {
>f : Symbol(f, Decl(classDeclarationBlockScoping2.ts, 0, 0))

class C {}
>C : Symbol(C, Decl(classDeclarationBlockScoping2.ts, 0, 14))

var c1 = C;
>c1 : Symbol(c1, Decl(classDeclarationBlockScoping2.ts, 2, 7))
>C : Symbol(C, Decl(classDeclarationBlockScoping2.ts, 0, 14))
{
class C {}
>C : Symbol(C, Decl(classDeclarationBlockScoping2.ts, 3, 5))

var c2 = C;
>c2 : Symbol(c2, Decl(classDeclarationBlockScoping2.ts, 5, 11))
>C : Symbol(C, Decl(classDeclarationBlockScoping2.ts, 3, 5))
}
return C === c1;
>C : Symbol(C, Decl(classDeclarationBlockScoping2.ts, 0, 14))
>c1 : Symbol(c1, Decl(classDeclarationBlockScoping2.ts, 2, 7))
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/classDeclarationBlockScoping2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/classDeclarationBlockScoping2.ts ===
function f() {
>f : () => boolean

class C {}
>C : C

var c1 = C;
>c1 : typeof C
>C : typeof C
{
class C {}
>C : C

var c2 = C;
>c2 : typeof C
>C : typeof C
}
return C === c1;
>C === c1 : boolean
>C : typeof C
>c1 : typeof C
}
18 changes: 0 additions & 18 deletions tests/baselines/reference/classExpressionTest1.errors.txt

This file was deleted.

35 changes: 35 additions & 0 deletions tests/baselines/reference/classExpressionTest1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/compiler/classExpressionTest1.ts ===
function M() {
>M : Symbol(M, Decl(classExpressionTest1.ts, 0, 0))

class C<X> {
>C : Symbol(C, Decl(classExpressionTest1.ts, 0, 14))
>X : Symbol(X, Decl(classExpressionTest1.ts, 1, 12))

f<T>() {
>f : Symbol(f, Decl(classExpressionTest1.ts, 1, 16))
>T : Symbol(T, Decl(classExpressionTest1.ts, 2, 10))

var t: T;
>t : Symbol(t, Decl(classExpressionTest1.ts, 3, 15))
>T : Symbol(T, Decl(classExpressionTest1.ts, 2, 10))

var x: X;
>x : Symbol(x, Decl(classExpressionTest1.ts, 4, 15))
>X : Symbol(X, Decl(classExpressionTest1.ts, 1, 12))

return { t, x };
>t : Symbol(t, Decl(classExpressionTest1.ts, 5, 20))
>x : Symbol(x, Decl(classExpressionTest1.ts, 5, 23))
}
}

var v = new C<number>();
>v : Symbol(v, Decl(classExpressionTest1.ts, 9, 7))
>C : Symbol(C, Decl(classExpressionTest1.ts, 0, 14))

return v.f<string>();
>v.f : Symbol(C.f, Decl(classExpressionTest1.ts, 1, 16))
>v : Symbol(v, Decl(classExpressionTest1.ts, 9, 7))
>f : Symbol(C.f, Decl(classExpressionTest1.ts, 1, 16))
}
38 changes: 38 additions & 0 deletions tests/baselines/reference/classExpressionTest1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=== tests/cases/compiler/classExpressionTest1.ts ===
function M() {
>M : () => { t: string; x: number; }

class C<X> {
>C : C<X>
>X : X

f<T>() {
>f : <T>() => { t: T; x: X; }
>T : T

var t: T;
>t : T
>T : T

var x: X;
>x : X
>X : X

return { t, x };
>{ t, x } : { t: T; x: X; }
>t : T
>x : X
}
}

var v = new C<number>();
>v : C<number>
>new C<number>() : C<number>
>C : typeof C

return v.f<string>();
>v.f<string>() : { t: string; x: number; }
>v.f : <T>() => { t: T; x: number; }
>v : C<number>
>f : <T>() => { t: T; x: number; }
}
9 changes: 0 additions & 9 deletions tests/baselines/reference/classInsideBlock.errors.txt

This file was deleted.

7 changes: 7 additions & 0 deletions tests/baselines/reference/classInsideBlock.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/conformance/classes/classDeclarations/classInsideBlock.ts ===
function foo() {
>foo : Symbol(foo, Decl(classInsideBlock.ts, 0, 0))

class C { }
>C : Symbol(C, Decl(classInsideBlock.ts, 0, 16))
}
7 changes: 7 additions & 0 deletions tests/baselines/reference/classInsideBlock.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/conformance/classes/classDeclarations/classInsideBlock.ts ===
function foo() {
>foo : () => void

class C { }
>C : C
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ tests/cases/compiler/functionsWithModifiersInBlocks1.ts(2,25): error TS1184: An
tests/cases/compiler/functionsWithModifiersInBlocks1.ts(3,4): error TS1184: Modifiers cannot appear here.
tests/cases/compiler/functionsWithModifiersInBlocks1.ts(3,20): error TS2393: Duplicate function implementation.
tests/cases/compiler/functionsWithModifiersInBlocks1.ts(4,4): error TS1184: Modifiers cannot appear here.
tests/cases/compiler/functionsWithModifiersInBlocks1.ts(4,12): error TS1029: 'export' modifier must precede 'declare' modifier.
tests/cases/compiler/functionsWithModifiersInBlocks1.ts(4,28): error TS2393: Duplicate function implementation.
tests/cases/compiler/functionsWithModifiersInBlocks1.ts(4,32): error TS1184: An implementation cannot be declared in ambient contexts.


==== tests/cases/compiler/functionsWithModifiersInBlocks1.ts (9 errors) ====
==== tests/cases/compiler/functionsWithModifiersInBlocks1.ts (8 errors) ====
{
declare function f() { }
~~~~~~~
Expand All @@ -26,8 +25,6 @@ tests/cases/compiler/functionsWithModifiersInBlocks1.ts(4,32): error TS1184: An
declare export function f() { }
~~~~~~~
!!! error TS1184: Modifiers cannot appear here.
~~~~~~
!!! error TS1029: 'export' modifier must precede 'declare' modifier.
~
!!! error TS2393: Duplicate function implementation.
~
Expand Down
23 changes: 7 additions & 16 deletions tests/baselines/reference/generatorTypeCheck39.errors.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(5,5): error TS1129: Statement expected.
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(5,6): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(5,16): error TS2304: Cannot find name 'yield'.
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(5,22): error TS1005: ',' expected.
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(9,1): error TS1128: Declaration or statement expected.
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(5,16): error TS1163: A 'yield' expression is only allowed in a generator body.
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts(7,13): error TS1163: A 'yield' expression is only allowed in a generator body.


==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts (5 errors) ====
==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck39.ts (2 errors) ====
function decorator(x: any) {
return y => { };
}
function* g() {
@decorator(yield 0)
~
!!! error TS1129: Statement expected.
~~~~~~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~~~
!!! error TS2304: Cannot find name 'yield'.
~
!!! error TS1005: ',' expected.
!!! error TS1163: A 'yield' expression is only allowed in a generator body.
class C {
x = yield 0;
~~~~~
!!! error TS1163: A 'yield' expression is only allowed in a generator body.
}
}
~
!!! error TS1128: Declaration or statement expected.
}
Loading