Skip to content

Commit 935b865

Browse files
authored
Add redeclare variable error message (#1987) (#2002)
1 parent 397ed55 commit 935b865

File tree

5 files changed

+13
-4
lines changed

5 files changed

+13
-4
lines changed

NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ under the licensing terms detailed in LICENSE:
4242
* Roman F. <70765447+romdotdog@users.noreply.github.com>
4343
* Joe Pea <trusktr@gmail.com>
4444
* Felipe Gasper <FGasper@users.noreply.github.com>
45+
* Congcong Cai <77575210+HerrCai0907@users.noreply.github.com>
4546

4647
Portions of this software are derived from third-party works licensed under
4748
the following terms:

src/compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,7 +3030,7 @@ export class Compiler extends DiagnosticEmitter {
30303030

30313031
if (initializerNode) {
30323032
let pendingElements = this.pendingElements;
3033-
let dummy = flow.addScopedDummyLocal(name, type); // pending dummy
3033+
let dummy = flow.addScopedDummyLocal(name, type, statement); // pending dummy
30343034
pendingElements.add(dummy);
30353035
initExpr = this.compileExpression(initializerNode, type, // reports
30363036
Constraints.CONV_IMPLICIT
@@ -3042,7 +3042,7 @@ export class Compiler extends DiagnosticEmitter {
30423042
// Otherwise infer type from initializer
30433043
} else if (initializerNode) {
30443044
let pendingElements = this.pendingElements;
3045-
let temp = flow.addScopedDummyLocal(name, Type.auto); // pending dummy
3045+
let temp = flow.addScopedDummyLocal(name, Type.auto, statement); // pending dummy
30463046
pendingElements.add(temp);
30473047
initExpr = this.compileExpression(initializerNode, Type.auto); // reports
30483048
pendingElements.delete(temp);

src/diagnosticMessages.generated.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export enum DiagnosticCode {
151151
A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged = 2434,
152152
Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses = 2445,
153153
Variable_0_used_before_its_declaration = 2448,
154+
Cannot_redeclare_block_scoped_variable_0 = 2451,
154155
The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly = 2453,
155156
Type_0_has_no_property_1 = 2460,
156157
The_0_operator_cannot_be_applied_to_type_1 = 2469,
@@ -333,6 +334,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
333334
case 2434: return "A namespace declaration cannot be located prior to a class or function with which it is merged.";
334335
case 2445: return "Property '{0}' is protected and only accessible within class '{1}' and its subclasses.";
335336
case 2448: return "Variable '{0}' used before its declaration.";
337+
case 2451: return "Cannot redeclare block-scoped variable '{0}'";
336338
case 2453: return "The type argument for type parameter '{0}' cannot be inferred from the usage. Consider specifying the type arguments explicitly.";
337339
case 2460: return "Type '{0}' has no property '{1}'.";
338340
case 2469: return "The '{0}' operator cannot be applied to type '{1}'.";

src/diagnosticMessages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
"A namespace declaration cannot be located prior to a class or function with which it is merged.": 2434,
150150
"Property '{0}' is protected and only accessible within class '{1}' and its subclasses.": 2445,
151151
"Variable '{0}' used before its declaration.": 2448,
152+
"Cannot redeclare block-scoped variable '{0}'" : 2451,
152153
"The type argument for type parameter '{0}' cannot be inferred from the usage. Consider specifying the type arguments explicitly.": 2453,
153154
"Type '{0}' has no property '{1}'.": 2460,
154155
"The '{0}' operator cannot be applied to type '{1}'.": 2469,

src/flow.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,16 @@ export class Flow {
444444
}
445445

446446
/** Adds a new scoped dummy local of the specified name. */
447-
addScopedDummyLocal(name: string, type: Type): Local {
447+
addScopedDummyLocal(name: string, type: Type, declarationNode: Node): Local {
448448
var scopedDummy = new Local(name, -1, type, this.parentFunction);
449449
var scopedLocals = this.scopedLocals;
450450
if (!scopedLocals) this.scopedLocals = scopedLocals = new Map();
451-
else assert(!scopedLocals.has(name));
451+
else if (scopedLocals.has(name)) {
452+
this.parentFunction.program.error(
453+
DiagnosticCode.Cannot_redeclare_block_scoped_variable_0,
454+
declarationNode.range, name
455+
);
456+
}
452457
scopedDummy.set(CommonFlags.SCOPED);
453458
scopedLocals.set(name, scopedDummy);
454459
return scopedDummy;

0 commit comments

Comments
 (0)