Skip to content

[6.2] [CS] Distinguish locators for generic args in addSpecializationConstraint #82340

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 1 commit into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 8 additions & 4 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,11 @@ namespace {
SmallVector<Type, 2> specializationArgTypes;
auto options =
TypeResolutionOptions(TypeResolverContext::InExpression);
for (auto specializationArg : specializationArgs) {
ConstraintLocatorBuilder locBuilder(locator);
for (auto idx : indices(specializationArgs)) {
auto specializationArg = specializationArgs[idx];
auto argLocator =
locBuilder.withPathElement(LocatorPathElt::GenericArgument(idx));
PackExpansionExpr *elementEnv = nullptr;
if (!OuterExpansions.empty()) {
options |= TypeResolutionFlags::AllowPackReferences;
Expand All @@ -1942,9 +1946,9 @@ namespace {
auto result = TypeResolution::resolveContextualType(
specializationArg, CurDC, options,
// Introduce type variables for unbound generics.
OpenUnboundGenericType(CS, locator),
HandlePlaceholderType(CS, locator),
OpenPackElementType(CS, locator, elementEnv));
OpenUnboundGenericType(CS, argLocator),
HandlePlaceholderType(CS, argLocator),
OpenPackElementType(CS, argLocator, elementEnv));
if (result->hasError()) {
auto &ctxt = CS.getASTContext();
result = PlaceholderType::get(ctxt, specializationArg);
Expand Down
13 changes: 13 additions & 0 deletions test/Macros/macro_explicit_generic_args.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ func test(i: Int) {
// expected-error@-1{{external macro implementation type 'A.B' could not be found for macro 'resolve'; plugin for module 'A' not found}}
}

@freestanding(expression)
macro OverloadedMacro<T, U>(_ x: T, _ y: U) // expected-error {{requires a definition}} expected-note {{declared here}}

@freestanding(expression)
macro OverloadedMacro<T, U>(_ x: T, _ y: U, z: Int = 0) // expected-error {{requires a definition}}

// Make sure we don't crash.
func testOverloadedMacro() {
struct S<T> {} // expected-note 2{{'T' declared as parameter to type 'S'}}
_ = #OverloadedMacro<S, S>
// expected-error@-1 2{{generic parameter 'T' could not be inferred}}
// expected-error@-2 {{missing arguments for parameters #1, #2 in macro expansion}}
}
9 changes: 9 additions & 0 deletions test/Sema/generic_specialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,12 @@ do {
// expected-error@+1:13 {{cannot specialize non-generic type 'module<Swift>'}}{{none}}
func f(_: Swift<Int>) {}
}

func overloadedGenericFn<T, U>(_ x: T, _ y: U) {} // expected-note {{found this candidate}}
func overloadedGenericFn<T, U>(_ x: T, _ y: U, z: Int = 0) {} // expected-note {{found this candidate}}

// Make sure we don't crash.
func testSpecializedOverloaded() {
struct S<T> {}
_ = overloadedGenericFn<S, S> // expected-error {{no exact matches}}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// {"signature":"swift::constraints::ConstraintSystem::recordOpenedTypes(swift::constraints::ConstraintLocatorBuilder, llvm::SmallVectorImpl<std::__1::pair<swift::GenericTypeParamType*, swift::TypeVariableType*>>&, bool)"}
// RUN: not %target-swift-frontend -typecheck %s
a<b> class a func a < c class b < d