Skip to content

fix: add redeclare variable error message (#1987) #2002

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 2 commits into from
Jul 27, 2021
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
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ under the licensing terms detailed in LICENSE:
* Roman F. <[email protected]>
* Joe Pea <[email protected]>
* Felipe Gasper <[email protected]>
* Congcong Cai <[email protected]>

Portions of this software are derived from third-party works licensed under
the following terms:
Expand Down
4 changes: 2 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3030,7 +3030,7 @@ export class Compiler extends DiagnosticEmitter {

if (initializerNode) {
let pendingElements = this.pendingElements;
let dummy = flow.addScopedDummyLocal(name, type); // pending dummy
let dummy = flow.addScopedDummyLocal(name, type, statement); // pending dummy
pendingElements.add(dummy);
initExpr = this.compileExpression(initializerNode, type, // reports
Constraints.CONV_IMPLICIT
Expand All @@ -3042,7 +3042,7 @@ export class Compiler extends DiagnosticEmitter {
// Otherwise infer type from initializer
} else if (initializerNode) {
let pendingElements = this.pendingElements;
let temp = flow.addScopedDummyLocal(name, Type.auto); // pending dummy
let temp = flow.addScopedDummyLocal(name, Type.auto, statement); // pending dummy
pendingElements.add(temp);
initExpr = this.compileExpression(initializerNode, Type.auto); // reports
pendingElements.delete(temp);
Expand Down
2 changes: 2 additions & 0 deletions src/diagnosticMessages.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export enum DiagnosticCode {
A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged = 2434,
Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses = 2445,
Variable_0_used_before_its_declaration = 2448,
Cannot_redeclare_block_scoped_variable_0 = 2451,
The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly = 2453,
Type_0_has_no_property_1 = 2460,
The_0_operator_cannot_be_applied_to_type_1 = 2469,
Expand Down Expand Up @@ -333,6 +334,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
case 2434: return "A namespace declaration cannot be located prior to a class or function with which it is merged.";
case 2445: return "Property '{0}' is protected and only accessible within class '{1}' and its subclasses.";
case 2448: return "Variable '{0}' used before its declaration.";
case 2451: return "Cannot redeclare block-scoped variable '{0}'";
case 2453: return "The type argument for type parameter '{0}' cannot be inferred from the usage. Consider specifying the type arguments explicitly.";
case 2460: return "Type '{0}' has no property '{1}'.";
case 2469: return "The '{0}' operator cannot be applied to type '{1}'.";
Expand Down
1 change: 1 addition & 0 deletions src/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"A namespace declaration cannot be located prior to a class or function with which it is merged.": 2434,
"Property '{0}' is protected and only accessible within class '{1}' and its subclasses.": 2445,
"Variable '{0}' used before its declaration.": 2448,
"Cannot redeclare block-scoped variable '{0}'" : 2451,
"The type argument for type parameter '{0}' cannot be inferred from the usage. Consider specifying the type arguments explicitly.": 2453,
"Type '{0}' has no property '{1}'.": 2460,
"The '{0}' operator cannot be applied to type '{1}'.": 2469,
Expand Down
9 changes: 7 additions & 2 deletions src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,16 @@ export class Flow {
}

/** Adds a new scoped dummy local of the specified name. */
addScopedDummyLocal(name: string, type: Type): Local {
addScopedDummyLocal(name: string, type: Type, declarationNode: Node): Local {
var scopedDummy = new Local(name, -1, type, this.parentFunction);
var scopedLocals = this.scopedLocals;
if (!scopedLocals) this.scopedLocals = scopedLocals = new Map();
else assert(!scopedLocals.has(name));
else if (scopedLocals.has(name)) {
this.parentFunction.program.error(
DiagnosticCode.Cannot_redeclare_block_scoped_variable_0,
declarationNode.range, name
);
}
scopedDummy.set(CommonFlags.SCOPED);
scopedLocals.set(name, scopedDummy);
return scopedDummy;
Expand Down