Skip to content

[SwiftLexicalLookup] New unqualified lookup implementation validation #77140

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 17 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
04f5f33
Merge remote-tracking branch 'upstream/main'
MAJKFL Oct 9, 2024
1d94844
Add `SwiftLexicalLookup` validation.
MAJKFL Oct 20, 2024
2cc0088
Switch to integer division in`addPaddingUpTo`. Rename magic constants…
MAJKFL Oct 23, 2024
ba004b6
Fix validation of names with end flag. Fix missing lib in cmake file.
MAJKFL Nov 1, 2024
4b544e9
Merge branch 'main' into swift-lexical-lookup-validation
MAJKFL Nov 13, 2024
8fc6951
Use `ConfiguredRegions` instead of `BuildConfiguration` with `LookupC…
MAJKFL Nov 13, 2024
3ef2312
Merge branch 'swift-lexical-lookup-validation' of https://github.com/…
MAJKFL Nov 13, 2024
f4a5069
Add a dummy `BridgedConsumedLookupResult` constructor for compatibili…
MAJKFL Nov 13, 2024
34c90f1
Merge remote-tracking branch 'upstream/main' into swift-lexical-looku…
MAJKFL Nov 14, 2024
d724230
Add casting of exported source file syntax to `SourceFileSyntax`.
MAJKFL Nov 14, 2024
bda3c7f
Use `BRIDGED_INLINE` to avoid dummy constructor in `BridgedConsumedLo…
MAJKFL Nov 15, 2024
5b39ba6
Merge branch 'main' into swift-lexical-lookup-validation
MAJKFL Nov 15, 2024
024f630
Add a basic SwiftLexicalLookup validation test. Add caching of `Confi…
MAJKFL Nov 26, 2024
0371783
Merge branch 'swift-lexical-lookup-validation' of https://github.com/…
MAJKFL Nov 26, 2024
866f1c1
Merge branch 'main' into swift-lexical-lookup-validation
MAJKFL Nov 26, 2024
0ba7a5e
Add `REQUIRES: UnqualifiedLookupValidation` comment to unqualified lo…
MAJKFL Dec 5, 2024
39e4d15
Add `REQUIRES: swift_feature_UnqualifiedLookupValidation` to the `Swi…
MAJKFL Dec 13, 2024
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
15 changes: 15 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ struct BridgedLocatedIdentifier {
BridgedSourceLoc NameLoc;
};

struct BridgedConsumedLookupResult {
SWIFT_NAME("name")
BridgedIdentifier Name;

SWIFT_NAME("nameLoc")
BridgedSourceLoc NameLoc;

SWIFT_NAME("flag")
SwiftInt Flag;

BRIDGED_INLINE BridgedConsumedLookupResult(swift::Identifier name,
swift::SourceLoc sourceLoc,
SwiftInt flag);
};

class BridgedDeclBaseName {
BridgedIdentifier Ident;

Expand Down
9 changes: 9 additions & 0 deletions include/swift/AST/ASTBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ swift::DeclBaseName BridgedDeclBaseName::unbridged() const {
return swift::DeclBaseName(Ident.unbridged());
}

//===----------------------------------------------------------------------===//
// MARK: BridgedDeclBaseName
//===----------------------------------------------------------------------===//

BridgedConsumedLookupResult::BridgedConsumedLookupResult(
swift::Identifier name, swift::SourceLoc sourceLoc, SwiftInt flag)
: Name(BridgedIdentifier(name)), NameLoc(BridgedSourceLoc(sourceLoc)),
Flag(flag) {}

//===----------------------------------------------------------------------===//
// MARK: BridgedDeclNameRef
//===----------------------------------------------------------------------===//
Expand Down
7 changes: 7 additions & 0 deletions include/swift/AST/DiagnosticsCommon.def
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ NOTE(in_macro_expansion,none,
ERROR(macro_experimental,none,
"%0 macros are an experimental feature that is not enabled %select{|(%1)}1",
(StringRef, StringRef))

//------------------------------------------------------------------------------
// MARK: lexical lookup diagnostics
//------------------------------------------------------------------------------

ERROR(lookup_outputs_dont_match,none,
"Unqualified lookup output from ASTScope and SwiftLexicalLookup don't match", ())

//------------------------------------------------------------------------------
// MARK: bridged diagnostics
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ EXPERIMENTAL_FEATURE(ParserRoundTrip, false)
/// Swift parser.
EXPERIMENTAL_FEATURE(ParserValidation, false)

/// Whether to perform validation of the unqualified lookup produced by
/// ASTScope and SwiftLexicalLookup
EXPERIMENTAL_FEATURE(UnqualifiedLookupValidation, false)

/// Enables implicit some while also enabling existential `any`
EXPERIMENTAL_FEATURE(ImplicitSome, false)

Expand Down
7 changes: 7 additions & 0 deletions include/swift/Bridging/ASTGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ intptr_t swift_ASTGen_configuredRegions(
void swift_ASTGen_freeConfiguredRegions(
BridgedIfConfigClauseRangeInfo *_Nullable regions, intptr_t numRegions);

bool swift_ASTGen_validateUnqualifiedLookup(
void *_Nonnull sourceFile,
BridgedASTContext astContext,
BridgedSourceLoc sourceLoc,
bool finishInSequentialScope,
BridgedArrayRef astScopeResultRef);

size_t
swift_ASTGen_virtualFiles(void *_Nonnull sourceFile,
BridgedVirtualFile *_Nullable *_Nonnull virtualFiles);
Expand Down
124 changes: 123 additions & 1 deletion lib/AST/ASTScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/Bridging/ASTGen.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Initializer.h"
Expand All @@ -39,6 +40,104 @@ using namespace ast_scope;

#pragma mark ASTScope

class LoggingASTScopeDeclConsumer
: public namelookup::AbstractASTScopeDeclConsumer {
private:
const int shouldLookInMembers = 0b10;
namelookup::AbstractASTScopeDeclConsumer *originalConsumer;

public:
mutable SmallVector<BridgedConsumedLookupResult> recordedElements;

LoggingASTScopeDeclConsumer(
namelookup::AbstractASTScopeDeclConsumer *consumer)
: originalConsumer(consumer) {}

~LoggingASTScopeDeclConsumer() = default;

/// Called for every ValueDecl visible from the lookup.
///
/// Takes an array in order to batch the consumption before setting
/// IndexOfFirstOuterResult when necessary.
///
/// Additionally, each name is logged to `recordedElements` and
/// can be later used in validation of `SwiftLexicalLookup` result.
///
/// \param baseDC either a type context or the local context of a
/// `self` parameter declaration. See LookupResult for a discussion
/// of type -vs- instance lookup results.
///
/// \return true if the lookup should be stopped at this point.
bool consume(ArrayRef<ValueDecl *> values,
NullablePtr<DeclContext> baseDC = nullptr) override {
bool endOfLookup = originalConsumer->consume(values, baseDC);

for (auto value : values) {
if (auto sourceLoc = value->getLoc()) {
recordedElements.push_back(BridgedConsumedLookupResult(
value->getBaseIdentifier(), sourceLoc, endOfLookup));
} else {
// If sourceLoc is unavailable, use location of it's parent.
recordedElements.push_back(BridgedConsumedLookupResult(
value->getBaseIdentifier(),
value->getDeclContext()->getAsDecl()->getLoc(), endOfLookup));
}
}

return endOfLookup;
};

/// Look for members of a nominal type or extension scope.
///
/// Each call is recorded in `recordedElements` with a special flag set.
/// It can be later used in validation of `SwiftLexicalLookup` result.
///
/// \return true if the lookup should be stopped at this point.
bool lookInMembers(const DeclContext *scopeDC) const override {
bool endOfLookup = originalConsumer->lookInMembers(scopeDC);

if (auto *extDecl = dyn_cast<ExtensionDecl>(scopeDC)) {
recordedElements.push_back(BridgedConsumedLookupResult(
Identifier(), extDecl->getExtendedTypeRepr()->getLoc(),
shouldLookInMembers + endOfLookup));
} else {
recordedElements.push_back(BridgedConsumedLookupResult(
scopeDC->getSelfNominalTypeDecl()->getBaseIdentifier(),
scopeDC->getAsDecl()->getLoc(), shouldLookInMembers + endOfLookup));
}

return endOfLookup;
};

/// Called for local VarDecls that might not yet be in scope.
///
/// Note that the set of VarDecls visited here are going to be a
/// superset of those visited in consume().
bool consumePossiblyNotInScope(ArrayRef<VarDecl *> values) override {
bool result = originalConsumer->consumePossiblyNotInScope(values);
return result;
}

/// Called right before looking at the parent scope of a BraceStmt.
///
/// \return true if the lookup should be stopped at this point.
bool finishLookupInBraceStmt(BraceStmt *stmt) override {
return originalConsumer->finishLookupInBraceStmt(stmt);
}

#ifndef NDEBUG
void startingNextLookupStep() override {
originalConsumer->startingNextLookupStep();
}
void finishingLookup(std::string input) const override {
originalConsumer->finishingLookup(input);
}
bool isTargetLookup() const override {
return originalConsumer->isTargetLookup();
}
#endif
};

void ASTScope::unqualifiedLookup(
SourceFile *SF, SourceLoc loc,
namelookup::AbstractASTScopeDeclConsumer &consumer) {
Expand All @@ -48,7 +147,30 @@ void ASTScope::unqualifiedLookup(

if (auto *s = SF->getASTContext().Stats)
++s->getFrontendCounters().NumASTScopeLookups;
ASTScopeImpl::unqualifiedLookup(SF, loc, consumer);

// Perform validation of SwiftLexicalLookup if option
// Feature::UnqualifiedLookupValidation is enabled and lookup was not
// performed in a macro.
if (SF->getASTContext().LangOpts.hasFeature(
Feature::UnqualifiedLookupValidation) &&
!SF->getEnclosingSourceFile()) {
LoggingASTScopeDeclConsumer loggingASTScopeDeclConsumer =
LoggingASTScopeDeclConsumer(&consumer);

ASTScopeImpl::unqualifiedLookup(SF, loc, loggingASTScopeDeclConsumer);

bool passed = swift_ASTGen_validateUnqualifiedLookup(
SF->getExportedSourceFile(), SF->getASTContext(), loc,
loggingASTScopeDeclConsumer.finishLookupInBraceStmt(nullptr),
BridgedArrayRef(loggingASTScopeDeclConsumer.recordedElements.data(),
loggingASTScopeDeclConsumer.recordedElements.size()));

if (!passed) {
SF->getASTContext().Diags.diagnose(loc, diag::lookup_outputs_dont_match);
}
} else {
ASTScopeImpl::unqualifiedLookup(SF, loc, consumer);
}
}

llvm::SmallVector<LabeledStmt *, 4> ASTScope::lookupLabeledStmts(
Expand Down
1 change: 1 addition & 0 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ UNINTERESTING_FEATURE(OpaqueTypeErasure)
UNINTERESTING_FEATURE(PackageCMO)
UNINTERESTING_FEATURE(ParserRoundTrip)
UNINTERESTING_FEATURE(ParserValidation)
UNINTERESTING_FEATURE(UnqualifiedLookupValidation)
UNINTERESTING_FEATURE(ImplicitSome)
UNINTERESTING_FEATURE(ParserASTGen)
UNINTERESTING_FEATURE(BuiltinMacros)
Expand Down
1 change: 1 addition & 0 deletions lib/ASTGen/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ let package = Package(
dependencies: [
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
.product(name: "SwiftIfConfig", package: "swift-syntax"),
.product(name: "SwiftLexicalLookup", package: "swift-syntax"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftParserDiagnostics", package: "swift-syntax"),
Expand Down
2 changes: 2 additions & 0 deletions lib/ASTGen/Sources/ASTGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_pure_swift_host_library(swiftASTGen STATIC CXX_INTEROP
DiagnosticsBridge.swift
Exprs.swift
Generics.swift
LexicalLookup.swift
Literals.swift
ParameterClause.swift
Patterns.swift
Expand All @@ -24,6 +25,7 @@ add_pure_swift_host_library(swiftASTGen STATIC CXX_INTEROP
_CompilerRegexParser
_CompilerSwiftSyntax
_CompilerSwiftIfConfig
_CompilerSwiftLexicalLookup
_CompilerSwiftOperators
_CompilerSwiftSyntaxBuilder
_CompilerSwiftParser
Expand Down
Loading