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

Fix resolving entity name of namespace member after alias is merged with type #53387

Merged
merged 2 commits into from
Mar 20, 2023

Conversation

andrewbranch
Copy link
Member

Fixes #51975. The root cause of the bug is essentially the same as the ones described in #50455.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Mar 20, 2023
src/compiler/checker.ts Outdated Show resolved Hide resolved
@@ -4683,6 +4683,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
symbol = getMergedSymbol(getSymbol(getExportsOfSymbol(namespace), right.escapedText, meaning));
if (!symbol && (namespace.flags & SymbolFlags.Alias)) {
// `namespace` can be resolved further if there was a symbol merge with a re-export
symbol = getMergedSymbol(getSymbol(getExportsOfSymbol(resolveAlias(namespace)), right.escapedText, meaning));
Copy link
Member

@jakebailey jakebailey Mar 20, 2023

Choose a reason for hiding this comment

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

Can this be simplified to just calling resolveSymbol in the original symbol = ... expression RHS above?

I feel like I fixed a bug like this recently that did something similar. That and there's getExportSymbolOfValueSymbolIfExported which I think is related and does some of this chain of calls. (I think we're somewhat inconsistent about it.)

Copy link
Member Author

Choose a reason for hiding this comment

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

No, because then we would fail to look up the property on the original value of namespace, which could very well be the one that has the export—it’s just not in this case. You could make a (somewhat more contrived) test where the alias points to a type and the local declaration is the namespace containing the export we’re looking for.

Copy link
Member

Choose a reason for hiding this comment

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

I think this also needs another condition, like

Suggested change
symbol = getMergedSymbol(getSymbol(getExportsOfSymbol(resolveAlias(namespace)), right.escapedText, meaning));
if (!getMergedSymbol(getSymbol(getExportsOfSymbol(namespace), right.escapedText, SymbolFlags.All))) {
symbol = getMergedSymbol(getSymbol(getExportsOfSymbol(resolveAlias(namespace)), right.escapedText, meaning));
}

since if that symbol exists in the original non-alias namespace (just with not the target meanings we're looking for), it should probably still block lookup in the alias target... right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I’m not sure it should, but it’s worth noting that this scenario you describe would be a merge error. In the test given, the non-alias symbol meaning is the type alias Something<A>—not a namespace at all. If we change it to be a namespace and try to reason about which of the two namespaces we want to pull from:

image

Copy link
Member

Choose a reason for hiding this comment

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

Are you sure you can't manage a similar shape in JS that we don't technically error on though? Something like

// @Filename: usage.ts
import { Something } from "./prelude"
export const myValue: Something<string> = Something.of("abc")
export type MyType = Something.SubType<string>

// @Filename: Something.ts
export type Something<A> = { value: A }
export type SubType<A> = { value: A }
export declare function of<A>(value: A): Something<A>

// @Filename: prelude.js
import * as S from "./Something"
export * as Something from "./Something"
export type Something<A> = S.Something<A>;
module.exports.Something = 12;

Copy link
Member Author

Choose a reason for hiding this comment

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

In that example, we basically seem to ignore the module.exports.Something assignment, both before and after this change. But just imagine that the double-namespace example I gave was not an error, and our emit did something that would make the runtime merge work as expected. If that were possible, then the following would be true:

  1. Exports declared in multiple locations of that merged namespace would need to be checked for mergeability the same way local namespace merges are checked, so duplicate values/types couldn’t clobber each other.
  2. If two declarations of the same-named export are allowed to merge, since one is a type and one is a value, I don’t see why the local one should block the alias one.

Saying that the local should block the alias would be predicated on the runtime merge not working, but there’s already a top-level error for that.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I guess that's true; JS merges already let weird things with totally undefined behavior happen anyway, so it's not really important how they influence this.

@andrewbranch andrewbranch merged commit f43a4fe into microsoft:main Mar 20, 2023
@andrewbranch andrewbranch deleted the bug/51975 branch March 20, 2023 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support a way to export type X and * as X as namespace with values and types
4 participants