Skip to content

Error on redeclarations of undefined #5420

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

Merged
merged 6 commits into from
Dec 5, 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
27 changes: 26 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace ts {
const emitResolver = createResolver();

const undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined");
undefinedSymbol.declarations = [];
const argumentsSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "arguments");

const checker: TypeChecker = {
Expand Down Expand Up @@ -234,6 +235,10 @@ namespace ts {
ResolvedReturnType
}

const builtinGlobals: SymbolTable = {
[undefinedSymbol.name]: undefinedSymbol
};

initializeTypeChecker();

return checker;
Expand Down Expand Up @@ -360,6 +365,24 @@ namespace ts {
}
}

function addToSymbolTable(target: SymbolTable, source: SymbolTable, message: DiagnosticMessage) {
for (const id in source) {
if (hasProperty(source, id)) {
if (hasProperty(target, id)) {
// Error on redeclarations
forEach(target[id].declarations, addDeclarationDiagnostic(id, message));
}
else {
target[id] = source[id];
}
}
}

function addDeclarationDiagnostic(id: string, message: DiagnosticMessage) {
return (declaration: Declaration) => diagnostics.add(createDiagnosticForNode(declaration, message, id));
}
}

function getSymbolLinks(symbol: Symbol): SymbolLinks {
if (symbol.flags & SymbolFlags.Transient) return <TransientSymbol>symbol;
const id = getSymbolId(symbol);
Expand Down Expand Up @@ -15331,10 +15354,12 @@ namespace ts {
}
});

// Setup global builtins
addToSymbolTable(globals, builtinGlobals, Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0);

getSymbolLinks(undefinedSymbol).type = undefinedType;
getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments");
getSymbolLinks(unknownSymbol).type = unknownType;
globals[undefinedSymbol.name] = undefinedSymbol;

// Initialize special types
globalArrayType = <GenericType>getGlobalType("Array", /*arity*/ 1);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,10 @@
"category": "Error",
"code": 2396
},
"Declaration name conflicts with built-in global identifier '{0}'.": {
"category": "Error",
"code": 2397
},
"Duplicate identifier '_this'. Compiler uses variable declaration '_this' to capture 'this' reference.": {
"category": "Error",
"code": 2399
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/undefinedTypeAssignment1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
tests/cases/compiler/undefinedTypeAssignment1.ts(1,1): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.


==== tests/cases/compiler/undefinedTypeAssignment1.ts (1 errors) ====
type undefined = string;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
function p(undefined = "wat") {
return undefined;
}

12 changes: 12 additions & 0 deletions tests/baselines/reference/undefinedTypeAssignment1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [undefinedTypeAssignment1.ts]
type undefined = string;
function p(undefined = "wat") {
return undefined;
}


//// [undefinedTypeAssignment1.js]
function p(undefined) {
if (undefined === void 0) { undefined = "wat"; }
return undefined;
}
8 changes: 8 additions & 0 deletions tests/baselines/reference/undefinedTypeAssignment2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests/cases/compiler/undefinedTypeAssignment2.ts(1,5): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.


==== tests/cases/compiler/undefinedTypeAssignment2.ts (1 errors) ====
var undefined = void 0;
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.

6 changes: 6 additions & 0 deletions tests/baselines/reference/undefinedTypeAssignment2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [undefinedTypeAssignment2.ts]
var undefined = void 0;


//// [undefinedTypeAssignment2.js]
var undefined = void 0;
8 changes: 8 additions & 0 deletions tests/baselines/reference/undefinedTypeAssignment3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests/cases/compiler/undefinedTypeAssignment3.ts(1,5): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.


==== tests/cases/compiler/undefinedTypeAssignment3.ts (1 errors) ====
var undefined = null;
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.

6 changes: 6 additions & 0 deletions tests/baselines/reference/undefinedTypeAssignment3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [undefinedTypeAssignment3.ts]
var undefined = null;


//// [undefinedTypeAssignment3.js]
var undefined = null;
24 changes: 24 additions & 0 deletions tests/baselines/reference/undefinedTypeAssignment4.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
tests/cases/compiler/undefinedTypeAssignment4.ts(1,7): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
tests/cases/compiler/undefinedTypeAssignment4.ts(4,11): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
tests/cases/compiler/undefinedTypeAssignment4.ts(7,11): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.


==== tests/cases/compiler/undefinedTypeAssignment4.ts (3 errors) ====
class undefined {
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
foo: string;
}
interface undefined {
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
member: number;
}
namespace undefined {
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
export var x = 42;
}
var x: undefined;
var y: typeof undefined;

26 changes: 26 additions & 0 deletions tests/baselines/reference/undefinedTypeAssignment4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [undefinedTypeAssignment4.ts]
class undefined {
foo: string;
}
interface undefined {
member: number;
}
namespace undefined {
export var x = 42;
}
var x: undefined;
var y: typeof undefined;


//// [undefinedTypeAssignment4.js]
var undefined = (function () {
function undefined() {
}
return undefined;
})();
var undefined;
(function (undefined) {
undefined.x = 42;
})(undefined || (undefined = {}));
var x;
var y;
4 changes: 4 additions & 0 deletions tests/cases/compiler/undefinedTypeAssignment1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type undefined = string;
function p(undefined = "wat") {
return undefined;
}
1 change: 1 addition & 0 deletions tests/cases/compiler/undefinedTypeAssignment2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var undefined = void 0;
1 change: 1 addition & 0 deletions tests/cases/compiler/undefinedTypeAssignment3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var undefined = null;
11 changes: 11 additions & 0 deletions tests/cases/compiler/undefinedTypeAssignment4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class undefined {
foo: string;
}
interface undefined {
member: number;
}
namespace undefined {
export var x = 42;
}
var x: undefined;
var y: typeof undefined;