Skip to content

Commit e6a668b

Browse files
authored
Fix assertion on duplicate imported identifiers (#1124)
1 parent aa1774c commit e6a668b

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/program.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ export class Program extends DiagnosticEmitter {
787787
// queued imports should be resolvable now through traversing exports and queued exports
788788
for (let i = 0, k = queuedImports.length; i < k; ++i) {
789789
let queuedImport = queuedImports[i];
790+
let localIdentifier = queuedImport.localIdentifier;
790791
let foreignIdentifier = queuedImport.foreignIdentifier;
791792
if (foreignIdentifier) { // i.e. import { foo [as bar] } from "./baz"
792793
let element = this.lookupForeign(
@@ -797,9 +798,9 @@ export class Program extends DiagnosticEmitter {
797798
);
798799
if (element) {
799800
queuedImport.localFile.add(
800-
queuedImport.localIdentifier.text,
801+
localIdentifier.text,
801802
element,
802-
true // isImport
803+
localIdentifier // isImport
803804
);
804805
} else {
805806
// FIXME: file not found is not reported if this happens?
@@ -812,14 +813,15 @@ export class Program extends DiagnosticEmitter {
812813
let foreignFile = this.lookupForeignFile(queuedImport.foreignPath, queuedImport.foreignPathAlt);
813814
if (foreignFile) {
814815
let localFile = queuedImport.localFile;
815-
let localName = queuedImport.localIdentifier.text;
816+
let localName = localIdentifier.text;
816817
localFile.add(
817818
localName,
818819
foreignFile.asImportedNamespace(
819820
localName,
820-
localFile
821+
localFile,
822+
localIdentifier
821823
),
822-
true // isImport
824+
localIdentifier // isImport
823825
);
824826
} else {
825827
assert(false); // already reported by the parser not finding the file
@@ -1775,7 +1777,7 @@ export class Program extends DiagnosticEmitter {
17751777
// resolve right away if the element exists
17761778
var element = this.lookupForeign(declaration.foreignName.text, foreignPath, foreignPathAlt, queuedExports);
17771779
if (element) {
1778-
parent.add(declaration.name.text, element, true);
1780+
parent.add(declaration.name.text, element, declaration.name /* isImport */);
17791781
return;
17801782
}
17811783

@@ -2161,7 +2163,7 @@ export abstract class Element {
21612163
abstract lookup(name: string): Element | null;
21622164

21632165
/** Adds an element as a member of this one. Reports and returns `false` if a duplicate. */
2164-
add(name: string, element: DeclaredElement): bool {
2166+
add(name: string, element: DeclaredElement, localIdentifierIfImport: IdentifierExpression | null = null): bool {
21652167
var originalDeclaration = element.declaration;
21662168
var members = this.members;
21672169
if (!members) this.members = members = new Map();
@@ -2174,17 +2176,18 @@ export abstract class Element {
21742176
if (merged) {
21752177
element = merged; // use merged element
21762178
} else {
2179+
let reportedIdentifier = localIdentifierIfImport || element.identifierNode;
21772180
if (isDeclaredElement(existing.kind)) {
21782181
this.program.errorRelated(
21792182
DiagnosticCode.Duplicate_identifier_0,
2180-
element.identifierNode.range,
2181-
(<DeclaredElement>existing).declaration.name.range,
2182-
element.identifierNode.text
2183+
reportedIdentifier.range,
2184+
(<DeclaredElement>existing).identifierNode.range,
2185+
reportedIdentifier.text
21832186
);
21842187
} else {
21852188
this.program.error(
21862189
DiagnosticCode.Duplicate_identifier_0,
2187-
element.identifierNode.range, element.identifierNode.text
2190+
reportedIdentifier.range, reportedIdentifier.text
21882191
);
21892192
}
21902193
return false;
@@ -2338,13 +2341,13 @@ export class File extends Element {
23382341
}
23392342

23402343
/* @override */
2341-
add(name: string, element: DeclaredElement, isImport: bool = false): bool {
2344+
add(name: string, element: DeclaredElement, localIdentifierIfImport: IdentifierExpression | null = null): bool {
23422345
if (element.hasDecorator(DecoratorFlags.GLOBAL)) {
23432346
element = this.program.ensureGlobal(name, element); // possibly merged globally
23442347
}
2345-
if (!super.add(name, element)) return false;
2348+
if (!super.add(name, element, localIdentifierIfImport)) return false;
23462349
element = assert(this.lookupInSelf(name)); // possibly merged locally
2347-
if (element.is(CommonFlags.EXPORT) && !isImport) {
2350+
if (element.is(CommonFlags.EXPORT) && !localIdentifierIfImport) {
23482351
this.ensureExport(
23492352
element.name,
23502353
element
@@ -2404,12 +2407,10 @@ export class File extends Element {
24042407
}
24052408

24062409
/** Creates an imported namespace from this file. */
2407-
asImportedNamespace(name: string, parent: Element): Namespace {
2408-
var ns = new Namespace(
2409-
name,
2410-
parent,
2411-
this.program.makeNativeNamespaceDeclaration(name)
2412-
);
2410+
asImportedNamespace(name: string, parent: Element, localIdentifier: IdentifierExpression): Namespace {
2411+
var declaration = this.program.makeNativeNamespaceDeclaration(name);
2412+
declaration.name = localIdentifier;
2413+
var ns = new Namespace(name, parent, declaration);
24132414
var exports = this.exports;
24142415
if (exports) {
24152416
for (let [memberName, member] of exports) {
@@ -3719,7 +3720,7 @@ function tryMerge(older: Element, newer: Element): DeclaredElement | null {
37193720
// NOTE: some of the following cases are not supported by TS, not sure why exactly.
37203721
// suggesting to just merge what seems to be possible for now and revisit later.
37213722
assert(older.program === newer.program);
3722-
assert(!newer.members);
3723+
if (newer.members) return null;
37233724
var merged: DeclaredElement | null = null;
37243725
switch (older.kind) {
37253726
case ElementKind.FUNCTION_PROTOTYPE: {

0 commit comments

Comments
 (0)