From 8102e5ee81db177372bb90188c65d003a4907aa4 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 6 Dec 2023 15:52:02 -0800 Subject: Change default visibility of interface members and update docs. (#3381) * Update behavior around interfaces and docs. * Update toc --------- Co-authored-by: Yong He --- source/slang/slang-ast-support-types.h | 12 ++++++++++++ source/slang/slang-check-decl.cpp | 12 ++++++++++++ source/slang/slang-check-expr.cpp | 13 +++++++++++++ source/slang/slang-diagnostic-defs.h | 2 +- source/slang/slang-syntax.cpp | 9 ++++++++- source/slang/slang.cpp | 6 ------ 6 files changed, 46 insertions(+), 8 deletions(-) (limited to 'source') diff --git a/source/slang/slang-ast-support-types.h b/source/slang/slang-ast-support-types.h index 1e71d0c4a..f771e24b6 100644 --- a/source/slang/slang-ast-support-types.h +++ b/source/slang/slang-ast-support-types.h @@ -66,6 +66,17 @@ namespace Slang void printDiagnosticArg(StringBuilder& sb, Val* val); void printDiagnosticArg(StringBuilder& sb, DeclRefBase* declRefBase); + struct QualifiedDeclPath + { + DeclRefBase* declRef; + QualifiedDeclPath() = default; + QualifiedDeclPath(DeclRefBase* declRef) + : declRef(declRef) + {} + }; + // Prints the fully qualified decl name. + void printDiagnosticArg(StringBuilder& sb, QualifiedDeclPath path); + class SyntaxNode; SourceLoc getDiagnosticPos(SyntaxNode const* syntax); @@ -1605,6 +1616,7 @@ namespace Slang Public, Default = Internal, }; + } // namespace Slang #endif diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 671bb26dd..39f0b9096 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -4059,7 +4059,19 @@ namespace Slang continue; if (doesMemberSatisfyRequirement(member.declRef, requiredMemberDeclRef, witnessTable)) + { + // The member satisfies the requirement in every other way except that + // it may have a lower visibility than min(parentVisibility, requirementVisibilty), + // in that case we will treat it as an error. + auto minRequiredVisibility = Math::Min(getDeclVisibility(requiredMemberDeclRef.getDecl()), getTypeVisibility(subType)); + if (getDeclVisibility(member.declRef.getDecl()) < minRequiredVisibility) + { + getSink()->diagnose(member.declRef, Diagnostics::satisfyingDeclCannotHaveLowerVisibility, member.declRef); + getSink()->diagnose(requiredMemberDeclRef, Diagnostics::seeDeclarationOf, QualifiedDeclPath(requiredMemberDeclRef)); + return false; + } return true; + } } // If we reach this point then there were no members suitable diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index dd868f70c..9d1898cf5 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -841,6 +841,12 @@ namespace Slang else if (as(modifier)) return DeclVisibility::Private; } + + // Interface members will always have the same visibility as the interface itself. + if (auto interfaceDecl = findParentInterfaceDecl(decl)) + { + return getDeclVisibility(interfaceDecl); + } if (auto parentModule = getModuleDecl(decl)) return parentModule->isInLegacyLanguage ? DeclVisibility::Public : DeclVisibility::Internal; @@ -919,6 +925,13 @@ namespace Slang { getSink()->diagnose(loc, Diagnostics::declIsNotVisible, lookupResult.item.declRef); outDiagnosed = true; + + if (getShared()->isInLanguageServer()) + { + // When in language server mode, return the unfiltered result so we can still + // provide language service around it. + return lookupResult; + } } return result; } diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h index c95d5ea98..ac3220f4b 100644 --- a/source/slang/slang-diagnostic-defs.h +++ b/source/slang/slang-diagnostic-defs.h @@ -360,7 +360,7 @@ DIAGNOSTIC(30505, Error, implementingMustReferencePrimaryModuleFile, "the source // Visibilty DIAGNOSTIC(30600, Error, declIsNotVisible, "'$0' is not accessible from the current context.") DIAGNOSTIC(30601, Error, declCannotHaveHigherVisibility, "'$0' cannot have a higher visibility than '$1'.") -DIAGNOSTIC(30602, Error, declCannotHaveLowerVisibility, "'$0' cannot have a lower visibility than '$1'.") +DIAGNOSTIC(30602, Error, satisfyingDeclCannotHaveLowerVisibility, "'$0' is less visible than the interface requirement it satisfies.") DIAGNOSTIC(30603, Error, invalidUseOfPrivateVisibility, "'$0' cannot have private visibility.") DIAGNOSTIC(30604, Error, useOfLessVisibleType, "'$0' references less visible type '$1'.") diff --git a/source/slang/slang-syntax.cpp b/source/slang/slang-syntax.cpp index 678d9ec26..6350a7e4d 100644 --- a/source/slang/slang-syntax.cpp +++ b/source/slang/slang-syntax.cpp @@ -2,7 +2,7 @@ #include "slang-compiler.h" #include "slang-visitor.h" - +#include "slang-ast-print.h" #include #include @@ -55,6 +55,13 @@ void printDiagnosticArg(StringBuilder& sb, QualType const& type) sb << ""; } +void printDiagnosticArg(StringBuilder& sb, QualifiedDeclPath path) +{ + ASTPrinter printer(getCurrentASTBuilder()); + printer.addDeclPath(path.declRef); + sb << printer.getString(); +} + SourceLoc getDiagnosticPos(SyntaxNode const* syntax) { if (!syntax) diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 0f53ffcd5..2852dd43e 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -1117,12 +1117,6 @@ SLANG_NO_THROW slang::IModule* SLANG_MCALL Linkage::loadModuleFromSource( if (SLANG_SUCCEEDED(Path::getCanonical(pathStr, cannonicalPath))) { pathInfo = PathInfo::makeNormal(pathStr, cannonicalPath); - ComPtr uniqueIdentity; - getFileSystemExt()->getFileUniqueIdentity(cannonicalPath.getBuffer(), uniqueIdentity.writeRef()); - if (uniqueIdentity && uniqueIdentity->getBufferSize() != 0) - { - pathInfo.uniqueIdentity = (char*)uniqueIdentity->getBufferPointer(); - } } } auto module = loadModule( -- cgit v1.2.3