Skip to content

Implement experimental module selectors (MyMod::someName) feature #34556

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

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ed76647
Use parseDeclNameRef in a few more places
beccadax May 8, 2025
3534654
[NFC] Add DeclNameLoc to specialize/dynamicReplacement
beccadax May 9, 2025
3b9e0ee
[NFC] Make ASTDumper more defensive
beccadax Aug 8, 2025
fb0461b
[NFC] Tweak source loc of @_implements error
beccadax Aug 12, 2025
99213ac
[NFC] Make pattern type checking more defensive
beccadax Aug 12, 2025
9a98834
[NFC-ish] Do lookahead without llvm::function_ref
beccadax Aug 13, 2025
53d7406
[NFC] Add tok::colon_colon to parser
beccadax Aug 13, 2025
8422e4a
Parse module selectors in permitted locations
beccadax Aug 13, 2025
8346bc6
Allow `::` as an alias for `.` in scoped imports
beccadax May 9, 2025
b458cf2
[NFC] [ASTGen] Don’t form PBDs without SourceLocs
beccadax Aug 6, 2025
1f092d6
Update and test ASTGen for module selectors
beccadax Aug 13, 2025
b5d9339
[NFC] Add skeleton of module selector lookup test
beccadax Aug 13, 2025
cc75d6d
[NFC] Improve some lookup-related doc comments
beccadax May 10, 2025
bbfe7bb
[NFC] Add representations for DeclNameRef/Loc with module selector
beccadax May 10, 2025
caa328a
[NFC] Audit some DeclName➡️DeclNameRef conversions
beccadax May 14, 2025
d72afad
Handle module selectors in qualified lookup
beccadax May 9, 2025
53446b5
Root unqualified lookups with module selectors at file scope
beccadax May 15, 2025
7cb3117
Handle module selectors in unqualified lookup
beccadax May 15, 2025
7b6f787
Handle module selectors in UDRE lookups
beccadax May 13, 2025
4be229e
FIXME operators unexpectedly passing
beccadax May 15, 2025
d2528bc
Handle module selectors with local vars right
beccadax May 8, 2025
d860c70
Improve module selector type lookup diagnostics
beccadax Aug 13, 2025
5af89a2
Improve module selector expr lookup diagnostics
beccadax Aug 13, 2025
8f492c9
Improve module selector constraint solver diagnostics
beccadax May 15, 2025
00d9ad7
Improve module selector @_dynamicReplacement diagnostics
beccadax May 10, 2025
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
60 changes: 36 additions & 24 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,29 @@ class BridgedDeclNameRef {
BRIDGED_INLINE swift::DeclNameRef unbridged() const;
};

SWIFT_NAME("BridgedDeclNameRef.createParsed(_:baseName:argumentLabels:)")
BridgedDeclNameRef BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
swift::DeclBaseName baseName,
BridgedArrayRef cLabels);
SWIFT_NAME("BridgedDeclNameRef.createParsed(_:moduleSelector:baseName:"
"argumentLabels:)")
BridgedDeclNameRef
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
swift::Identifier cModuleSelector,
swift::DeclBaseName cBaseName,
BridgedArrayRef cLabels);

SWIFT_NAME("BridgedDeclNameRef.createParsed(_:)")
SWIFT_NAME("BridgedDeclNameRef.createParsed(_:moduleSelector:baseName:)")
BridgedDeclNameRef
BridgedDeclNameRef_createParsed(swift::DeclBaseName baseName);
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
swift::Identifier cModuleSelector,
swift::DeclBaseName cBaseName);

class BridgedDeclNameLoc {
const void *_Nullable LocationInfo;
size_t NumArgumentLabels;
uint32_t NumArgumentLabels;
bool HasModuleSelectorLoc;

public:
BridgedDeclNameLoc() : LocationInfo(nullptr), NumArgumentLabels(0) {}
BridgedDeclNameLoc()
: LocationInfo(nullptr), NumArgumentLabels(0), HasModuleSelectorLoc(false)
{}

BRIDGED_INLINE BridgedDeclNameLoc(swift::DeclNameLoc loc);

Expand Down Expand Up @@ -941,11 +949,12 @@ BridgedDocumentationAttr BridgedDocumentationAttr_createParsed(

SWIFT_NAME(
"BridgedDynamicReplacementAttr.createParsed(_:atLoc:attrNameLoc:lParenLoc:"
"replacedFunction:rParenLoc:)")
"replacedFunction:replacedFunctionLoc:rParenLoc:)")
BridgedDynamicReplacementAttr BridgedDynamicReplacementAttr_createParsed(
BridgedASTContext cContext, swift::SourceLoc atLoc,
swift::SourceLoc attrNameLoc, swift::SourceLoc lParenLoc,
BridgedDeclNameRef cReplacedFunction, swift::SourceLoc rParenLoc);
BridgedASTContext cContext, swift::SourceLoc cAtLoc,
swift::SourceLoc cAttrNameLoc, swift::SourceLoc cLParenLoc,
BridgedDeclNameRef cReplacedFunction,
BridgedDeclNameLoc cReplacedFunctionLoc, swift::SourceLoc cRParenLoc);

SWIFT_NAME("BridgedEffectsAttr.createParsed(_:atLoc:range:effectKind:)")
BridgedEffectsAttr BridgedEffectsAttr_createParsed(
Expand Down Expand Up @@ -1276,22 +1285,24 @@ enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedSpecializationKind : uint8_t {
};

SWIFT_NAME("BridgedSpecializeAttr.createParsed(_:atLoc:range:whereClause:"
"exported:kind:taretFunction:spiGroups:availableAttrs:)")
"exported:kind:targetFunction:targetFunctionLoc:spiGroups:"
"availableAttrs:)")
BridgedSpecializeAttr BridgedSpecializeAttr_createParsed(
BridgedASTContext cContext, swift::SourceLoc atLoc,
swift::SourceRange range, BridgedNullableTrailingWhereClause cWhereClause,
bool exported, BridgedSpecializationKind cKind,
BridgedDeclNameRef cTargetFunction, BridgedArrayRef cSPIGroups,
BridgedArrayRef cAvailableAttrs);
BridgedDeclNameRef cTargetFunction, BridgedDeclNameLoc cTargetFunctionLoc,
BridgedArrayRef cSPIGroups, BridgedArrayRef cAvailableAttrs);

SWIFT_NAME("BridgedSpecializedAttr.createParsed(_:atLoc:range:whereClause:"
"exported:kind:taretFunction:spiGroups:availableAttrs:)")
"exported:kind:targetFunction:targetFunctionLoc:spiGroups:"
"availableAttrs:)")
BridgedSpecializedAttr BridgedSpecializedAttr_createParsed(
BridgedASTContext cContext, swift::SourceLoc atLoc,
swift::SourceRange range, BridgedNullableTrailingWhereClause cWhereClause,
bool exported, BridgedSpecializationKind cKind,
BridgedDeclNameRef cTargetFunction, BridgedArrayRef cSPIGroups,
BridgedArrayRef cAvailableAttrs);
BridgedDeclNameRef cTargetFunction, BridgedDeclNameLoc cTargetFunctionLoc,
BridgedArrayRef cSPIGroups, BridgedArrayRef cAvailableAttrs);

SWIFT_NAME(
"BridgedSPIAccessControlAttr.createParsed(_:atLoc:range:spiGroupName:)")
Expand Down Expand Up @@ -2544,9 +2555,10 @@ BridgedOpaqueReturnTypeOfTypeAttr_createParsed(
// MARK: TypeReprs
//===----------------------------------------------------------------------===//

SWIFT_NAME("BridgedUnqualifiedIdentTypeRepr.createParsed(_:loc:name:)")
SWIFT_NAME("BridgedUnqualifiedIdentTypeRepr.createParsed(_:name:loc:)")
BridgedUnqualifiedIdentTypeRepr BridgedUnqualifiedIdentTypeRepr_createParsed(
BridgedASTContext cContext, swift::SourceLoc loc, swift::Identifier id);
BridgedASTContext cContext, BridgedDeclNameRef cName,
BridgedDeclNameLoc cLoc);

SWIFT_NAME(
"BridgedArrayTypeRepr.createParsed(_:base:leftSquareLoc:rightSquareLoc:)")
Expand Down Expand Up @@ -2580,8 +2592,8 @@ BridgedCompileTimeLiteralTypeRepr_createParsed(BridgedASTContext cContext,
SWIFT_NAME("BridgedDeclRefTypeRepr.createParsed(_:base:name:nameLoc:"
"genericArguments:angleRange:)")
BridgedDeclRefTypeRepr BridgedDeclRefTypeRepr_createParsed(
BridgedASTContext cContext, BridgedTypeRepr cBase, swift::Identifier name,
swift::SourceLoc loc, BridgedArrayRef cGenericArguments,
BridgedASTContext cContext, BridgedTypeRepr cBase, BridgedDeclNameRef cName,
BridgedDeclNameLoc cLoc, BridgedArrayRef cGenericArguments,
swift::SourceRange angleRange);

SWIFT_NAME("BridgedDictionaryTypeRepr.createParsed(_:leftSquareLoc:keyType:"
Expand All @@ -2606,8 +2618,8 @@ BridgedFunctionTypeRepr BridgedFunctionTypeRepr_createParsed(
SWIFT_NAME("BridgedUnqualifiedIdentTypeRepr.createParsed(_:name:nameLoc:"
"genericArgs:leftAngleLoc:rightAngleLoc:)")
BridgedUnqualifiedIdentTypeRepr BridgedUnqualifiedIdentTypeRepr_createParsed(
BridgedASTContext cContext, swift::Identifier name,
swift::SourceLoc nameLoc, BridgedArrayRef genericArgs,
BridgedASTContext cContext, BridgedDeclNameRef cName,
BridgedDeclNameLoc cNameLoc, BridgedArrayRef genericArgs,
swift::SourceLoc lAngleLoc, swift::SourceLoc rAngleLoc);

SWIFT_NAME("BridgedOptionalTypeRepr.createParsed(_:base:questionLoc:)")
Expand Down
6 changes: 4 additions & 2 deletions include/swift/AST/ASTBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ swift::DeclNameRef BridgedDeclNameRef::unbridged() const {

BridgedDeclNameLoc::BridgedDeclNameLoc(swift::DeclNameLoc loc)
: LocationInfo(loc.LocationInfo),
NumArgumentLabels(loc.NumArgumentLabels) {}
NumArgumentLabels(loc.NumArgumentLabels),
HasModuleSelectorLoc(loc.HasModuleSelectorLoc) {}

swift::DeclNameLoc BridgedDeclNameLoc::unbridged() const {
return swift::DeclNameLoc(LocationInfo, NumArgumentLabels);
return swift::DeclNameLoc(LocationInfo, NumArgumentLabels,
HasModuleSelectorLoc);
}

//===----------------------------------------------------------------------===//
Expand Down
45 changes: 34 additions & 11 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1253,19 +1253,22 @@ class DynamicReplacementAttr final
friend class DynamicallyReplacedDeclRequest;

DeclNameRef ReplacedFunctionName;
DeclNameLoc ReplacedFunctionNameLoc;
LazyMemberLoader *Resolver = nullptr;
uint64_t ResolverContextData;

/// Create an @_dynamicReplacement(for:) attribute written in the source.
DynamicReplacementAttr(SourceLoc atLoc, SourceRange baseRange,
DeclNameRef replacedFunctionName,
DeclNameLoc replacedFunctionNameLoc,
SourceRange parenRange);

DynamicReplacementAttr(DeclNameRef name, AbstractFunctionDecl *f)
: DeclAttribute(DeclAttrKind::DynamicReplacement, SourceLoc(),
SourceRange(),
/*Implicit=*/false),
ReplacedFunctionName(name), Resolver(nullptr), ResolverContextData(0) {
ReplacedFunctionName(name), ReplacedFunctionNameLoc(),
Resolver(nullptr), ResolverContextData(0) {
Bits.DynamicReplacementAttr.HasTrailingLocationInfo = false;
}

Expand All @@ -1274,8 +1277,8 @@ class DynamicReplacementAttr final
: DeclAttribute(DeclAttrKind::DynamicReplacement, SourceLoc(),
SourceRange(),
/*Implicit=*/false),
ReplacedFunctionName(name), Resolver(Resolver),
ResolverContextData(Data) {
ReplacedFunctionName(name), ReplacedFunctionNameLoc(),
Resolver(Resolver), ResolverContextData(Data) {
Bits.DynamicReplacementAttr.HasTrailingLocationInfo = false;
}

Expand All @@ -1296,7 +1299,8 @@ class DynamicReplacementAttr final
public:
static DynamicReplacementAttr *
create(ASTContext &Context, SourceLoc AtLoc, SourceLoc DynReplLoc,
SourceLoc LParenLoc, DeclNameRef replacedFunction, SourceLoc RParenLoc);
SourceLoc LParenLoc, DeclNameRef replacedFunction,
DeclNameLoc replacedFunctionNameLoc, SourceLoc RParenLoc);

static DynamicReplacementAttr *create(ASTContext &ctx,
DeclNameRef replacedFunction,
Expand All @@ -1311,6 +1315,10 @@ class DynamicReplacementAttr final
return ReplacedFunctionName;
}

DeclNameLoc getReplacedFunctionNameLoc() const {
return ReplacedFunctionNameLoc;
}

/// Retrieve the location of the opening parentheses, if there is one.
SourceLoc getLParenLoc() const;

Expand Down Expand Up @@ -1800,6 +1808,7 @@ class AbstractSpecializeAttr
GenericSignature specializedSignature;

DeclNameRef targetFunctionName;
DeclNameLoc targetFunctionNameLoc;
LazyMemberLoader *resolver = nullptr;
uint64_t resolverContextData;
size_t numSPIGroups;
Expand All @@ -1812,7 +1821,9 @@ class AbstractSpecializeAttr
TrailingWhereClause *clause,
bool exported,
SpecializationKind kind, GenericSignature specializedSignature,
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
DeclNameRef targetFunctionName,
DeclNameLoc targetFunctionNameLoc,
ArrayRef<Identifier> spiGroups,
ArrayRef<AvailableAttr *> availabilityAttrs,
size_t typeErasedParamsCount);

Expand Down Expand Up @@ -1896,6 +1907,10 @@ class AbstractSpecializeAttr
return targetFunctionName;
}

DeclNameLoc getTargetFunctionNameLoc() const {
return targetFunctionNameLoc;
}

/// \p forDecl is the value decl that the attribute belongs to.
ValueDecl *getTargetFunctionDecl(const ValueDecl *forDecl) const;

Expand Down Expand Up @@ -1925,19 +1940,23 @@ class SpecializeAttr final : public AbstractSpecializeAttr,
TrailingWhereClause *clause,
bool exported,
SpecializationKind kind, GenericSignature specializedSignature,
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
DeclNameRef targetFunctionName,
DeclNameLoc targetFunctionNameLoc,
ArrayRef<Identifier> spiGroups,
ArrayRef<AvailableAttr *> availabilityAttrs,
size_t typeErasedParamsCount) :
AbstractSpecializeAttr(DeclAttrKind::Specialize, atLoc, Range, clause,
exported, kind, specializedSignature, targetFunctionName,
spiGroups, availabilityAttrs, typeErasedParamsCount) {}
targetFunctionNameLoc, spiGroups, availabilityAttrs,
typeErasedParamsCount) {}

public:
static SpecializeAttr *
create(ASTContext &Ctx, SourceLoc atLoc, SourceRange Range,
TrailingWhereClause *clause, bool exported,
SpecializationKind kind,
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
DeclNameRef targetFunctionName, DeclNameLoc targetFunctionNameLoc,
ArrayRef<Identifier> spiGroups,
ArrayRef<AvailableAttr *> availabilityAttrs,
GenericSignature specializedSignature = nullptr);

Expand Down Expand Up @@ -1981,19 +2000,23 @@ class SpecializedAttr final : public AbstractSpecializeAttr ,
TrailingWhereClause *clause,
bool exported,
SpecializationKind kind, GenericSignature specializedSignature,
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
DeclNameRef targetFunctionName,
DeclNameLoc targetFunctionNameLoc,
ArrayRef<Identifier> spiGroups,
ArrayRef<AvailableAttr *> availabilityAttrs,
size_t typeErasedParamsCount) :
AbstractSpecializeAttr(DeclAttrKind::Specialized, atLoc, Range, clause,
exported, kind, specializedSignature, targetFunctionName,
spiGroups, availabilityAttrs, typeErasedParamsCount) {}
targetFunctionNameLoc, spiGroups, availabilityAttrs,
typeErasedParamsCount) {}

public:
static SpecializedAttr *
create(ASTContext &Ctx, SourceLoc atLoc, SourceRange Range,
TrailingWhereClause *clause, bool exported,
SpecializationKind kind,
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
DeclNameRef targetFunctionName, DeclNameLoc targetFunctionNameLoc,
ArrayRef<Identifier> spiGroups,
ArrayRef<AvailableAttr *> availabilityAttrs,
GenericSignature specializedSignature = nullptr);

Expand Down
9 changes: 4 additions & 5 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3038,11 +3038,10 @@ class ValueDecl : public Decl {
return Name.getBaseIdentifier();
}

/// Generates a DeclNameRef referring to this declaration with as much
/// specificity as possible.
DeclNameRef createNameRef() const {
return DeclNameRef(Name);
}
/// Generates a DeclNameRef referring to this declaration.
///
/// \param moduleSelector If true, the name ref includes the module name.
DeclNameRef createNameRef(bool moduleSelector = false) const;

/// Retrieve the C declaration name that names this function, or empty
/// string if it has none.
Expand Down
Loading