Warn about module name shadowing in interfaces

There is a known issue with module interfaces where a type with the same name as a module will disrupt references to types in that module. Fully fixing it will require a new language feature (SR-898) which is not yet available. In the meantime, module interfaces support a workaround flag (“-Xfrontend -module-interface-preserve-types-as-written”) which prints an alternate form that usually works. However, you have to know to add this flag, and it’s not obvious because nothing breaks until a compiler tries to consume the affected module interface (or sometimes even one of its clients).

This commit emits a warning during module interface emission whenever the module interface either imports a type with the same name as the module being built, or declares a type with the same name as a visible module. This lets the user know that the type may cause problems and they might need to implement a workaround.
This commit is contained in:
Becca Royal-Gordon
2021-02-10 19:03:06 -08:00
parent cfa2983d7d
commit c2f3725f56
6 changed files with 142 additions and 0 deletions

View File

@@ -2051,6 +2051,8 @@ bool SourceFile::isImportedImplementationOnly(const ModuleDecl *module) const {
}
bool ModuleDecl::isImportedImplementationOnly(const ModuleDecl *module) const {
if (module == this) return false;
auto &imports = getASTContext().getImportCache();
// Look through non-implementation-only imports to see if module is imported