Skip to content
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
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4606,8 +4606,8 @@ NOTE(duplicated_key_declared_here, none,
"duplicate key declared here", ())

// Generic specializations
ERROR(cannot_explicitly_specialize_generic_function,none,
"cannot explicitly specialize a generic function", ())
ERROR(cannot_explicitly_specialize_function,none,
"cannot explicitly specialize %kind0", (ValueDecl *))
ERROR(not_a_generic_type,none,
"cannot specialize non-generic type %0", (Type))
ERROR(not_a_generic_macro,none,
Expand Down
20 changes: 11 additions & 9 deletions include/swift/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ enum class FixKind : uint8_t {
/// Ignore an attempt to specialize a non-generic type.
AllowConcreteTypeSpecialization,

/// Ignore an attempt to specialize a generic function.
AllowGenericFunctionSpecialization,
/// Ignore an attempt to specialize a (generic) function reference.
AllowFunctionSpecialization,

/// Ignore an out-of-place \c then statement.
IgnoreOutOfPlaceThenStmt,
Expand Down Expand Up @@ -3750,17 +3750,19 @@ class AllowConcreteTypeSpecialization final : public ConstraintFix {
}
};

class AllowGenericFunctionSpecialization final : public ConstraintFix {
class AllowFunctionSpecialization final : public ConstraintFix {
ValueDecl *Decl;

AllowGenericFunctionSpecialization(ConstraintSystem &cs, ValueDecl *decl,
ConstraintLocator *locator)
: ConstraintFix(cs, FixKind::AllowGenericFunctionSpecialization, locator),
AllowFunctionSpecialization(ConstraintSystem &cs, ValueDecl *decl,
ConstraintLocator *locator,
FixBehavior fixBehavior)
: ConstraintFix(cs, FixKind::AllowFunctionSpecialization, locator,
fixBehavior),
Decl(decl) {}

public:
std::string getName() const override {
return "allow generic function specialization";
return "allow (generic) function specialization";
}

bool diagnose(const Solution &solution, bool asNote = false) const override;
Expand All @@ -3769,11 +3771,11 @@ class AllowGenericFunctionSpecialization final : public ConstraintFix {
return diagnose(*commonFixes.front().first);
}

static AllowGenericFunctionSpecialization *
static AllowFunctionSpecialization *
create(ConstraintSystem &cs, ValueDecl *decl, ConstraintLocator *locator);

static bool classof(const ConstraintFix *fix) {
return fix->getKind() == FixKind::AllowGenericFunctionSpecialization;
return fix->getKind() == FixKind::AllowFunctionSpecialization;
}
};

Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9365,8 +9365,8 @@ bool ConcreteTypeSpecialization::diagnoseAsError() {
return true;
}

bool GenericFunctionSpecialization::diagnoseAsError() {
emitDiagnostic(diag::cannot_explicitly_specialize_generic_function);
bool InvalidFunctionSpecialization::diagnoseAsError() {
emitDiagnostic(diag::cannot_explicitly_specialize_function, Decl);
emitDiagnosticAt(Decl, diag::decl_declared_here, Decl);
return true;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -3133,13 +3133,15 @@ class ConcreteTypeSpecialization final : public FailureDiagnostic {
bool diagnoseAsError() override;
};

class GenericFunctionSpecialization final : public FailureDiagnostic {
/// Diagnose attempts to specialize (generic) function references.
class InvalidFunctionSpecialization final : public FailureDiagnostic {
ValueDecl *Decl;

public:
GenericFunctionSpecialization(const Solution &solution, ValueDecl *decl,
ConstraintLocator *locator)
: FailureDiagnostic(solution, locator), Decl(decl) {}
InvalidFunctionSpecialization(const Solution &solution, ValueDecl *decl,
ConstraintLocator *locator,
FixBehavior fixBehavior)
: FailureDiagnostic(solution, locator, fixBehavior), Decl(decl) {}

bool diagnoseAsError() override;
};
Expand Down
17 changes: 11 additions & 6 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2618,16 +2618,21 @@ AllowConcreteTypeSpecialization *AllowConcreteTypeSpecialization::create(
cs, concreteTy, decl, locator, fixBehavior);
}

bool AllowGenericFunctionSpecialization::diagnose(const Solution &solution,
bool asNote) const {
GenericFunctionSpecialization failure(solution, Decl, getLocator());
bool AllowFunctionSpecialization::diagnose(const Solution &solution,
bool asNote) const {
InvalidFunctionSpecialization failure(solution, Decl, getLocator(),
fixBehavior);
return failure.diagnose(asNote);
}

AllowGenericFunctionSpecialization *AllowGenericFunctionSpecialization::create(
ConstraintSystem &cs, ValueDecl *decl, ConstraintLocator *locator) {
AllowFunctionSpecialization *
AllowFunctionSpecialization::create(ConstraintSystem &cs, ValueDecl *decl,
ConstraintLocator *locator) {
auto fixBehavior = cs.getASTContext().isSwiftVersionAtLeast(6)
? FixBehavior::Error
: FixBehavior::DowngradeToWarning;
return new (cs.getAllocator())
AllowGenericFunctionSpecialization(cs, decl, locator);
AllowFunctionSpecialization(cs, decl, locator, fixBehavior);
}

bool IgnoreOutOfPlaceThenStmt::diagnose(const Solution &solution,
Expand Down
14 changes: 9 additions & 5 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14069,6 +14069,13 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(

auto genericParams = getGenericParams(decl);
if (!decl->getAsGenericContext() || !genericParams) {
if (isa<AbstractFunctionDecl>(decl)) {
return recordFix(AllowFunctionSpecialization::create(
*this, decl, getConstraintLocator(locator)))
? SolutionKind::Error
: SolutionKind::Solved;
}

// Allow concrete macros to have specializations with just a warning.
return recordFix(AllowConcreteTypeSpecialization::create(
*this, type1, decl, getConstraintLocator(locator),
Expand Down Expand Up @@ -14111,10 +14118,7 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
// FIXME: We could support explicit function specialization.
if (openedGenericParams.empty() ||
(isa<AbstractFunctionDecl>(decl) && !hasParameterPack)) {
if (!shouldAttemptFixes())
return SolutionKind::Error;

return recordFix(AllowGenericFunctionSpecialization::create(
return recordFix(AllowFunctionSpecialization::create(
*this, decl, getConstraintLocator(locator)))
? SolutionKind::Error
: SolutionKind::Solved;
Expand Down Expand Up @@ -15246,7 +15250,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
case FixKind::AllowAssociatedValueMismatch:
case FixKind::GenericArgumentsMismatch:
case FixKind::AllowConcreteTypeSpecialization:
case FixKind::AllowGenericFunctionSpecialization:
case FixKind::AllowFunctionSpecialization:
case FixKind::IgnoreGenericSpecializationArityMismatch:
case FixKind::IgnoreKeyPathSubscriptIndexMismatch:
case FixKind::AllowMemberRefOnExistential: {
Expand Down
7 changes: 5 additions & 2 deletions test/Parse/generic_disambiguation.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -verify-additional-prefix swift5-
// RUN: %target-typecheck-verify-swift -swift-version 6 -verify-additional-prefix swift6-

struct A<B> { // expected-note{{generic type 'A' declared here}}
init(x:Int) {}
Expand Down Expand Up @@ -40,7 +41,9 @@ _ = (a < b, c > d)
_ = a>(b)
_ = a > (b)

generic<Int>(0) // expected-error{{cannot explicitly specialize a generic function}}
generic<Int>(0)
// expected-swift5-warning@-1{{cannot explicitly specialize global function 'generic'}}
// expected-swift6-error@-2 {{cannot explicitly specialize global function 'generic'}}

A<B>.c()
A<A<B>>.c()
Expand Down
50 changes: 38 additions & 12 deletions test/Sema/generic_specialization.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -verify-additional-prefix swift5-
// RUN: %target-typecheck-verify-swift -swift-version 6 -verify-additional-prefix swift6-

extension Int {
func foo() -> Int {}
func foo() -> Int {} // expected-note 2 {{'foo()' declared here}}
var bar: Int {
get {}
}
Expand All @@ -14,28 +15,53 @@ extension Int {

// https://github.com/swiftlang/swift/issues/74857
func test(i: Int) {
let _ = i.foo<Int>() // expected-error {{cannot specialize non-generic type '() -> Int'}}
let _ = i.foo<Int>()
// expected-swift5-warning@-1 {{cannot explicitly specialize instance method 'foo()'}}
// expected-swift6-error@-2 {{cannot explicitly specialize instance method 'foo()'}}

let _ = i.gen<Int>() // expected-error {{cannot explicitly specialize a generic function}}
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
let _ = i.gen<Int>()
// expected-swift5-warning@-1 {{cannot explicitly specialize instance method 'gen()'}}
// expected-swift6-error@-2 {{cannot explicitly specialize instance method 'gen()'}}
// expected-error@-3 {{generic parameter 'T' could not be inferred}}

let _ = 0.foo<Int>() // expected-error {{cannot specialize non-generic type '() -> Int'}}
let _ = 0.foo<Int>()
// expected-swift5-warning@-1 {{cannot explicitly specialize instance method 'foo()'}}
// expected-swift6-error@-2 {{cannot explicitly specialize instance method 'foo()'}}

let _ = i.gen<Int> // expected-error {{cannot explicitly specialize a generic function}}
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
let _ = i.bar<Int> // expected-error {{cannot specialize non-generic type 'Int'}}
let _ = 0.bar<Int> // expected-error {{cannot specialize non-generic type 'Int'}}
let _ = i.gen<Int>
// expected-swift5-warning@-1 {{cannot explicitly specialize instance method 'gen()'}}
// expected-swift6-error@-2 {{cannot explicitly specialize instance method 'gen()'}}
// expected-error@-3 {{generic parameter 'T' could not be inferred}}
let _ = i.bar<Int>
// expected-swift5-error@-1 {{cannot specialize non-generic type 'Int'}}
// expected-swift6-error@-2 {{cannot specialize non-generic type 'Int'}}
let _ = 0.bar<Int>
// expected-swift5-error@-1 {{cannot specialize non-generic type 'Int'}}
// expected-swift6-error@-2 {{cannot specialize non-generic type 'Int'}}
}

extension Bool {
func foo<T>() -> T {} // expected-note {{'foo()' declared here}}
}

let _: () -> Bool = false.foo<Int> // expected-error {{cannot explicitly specialize a generic function}}
let _: () -> Bool = false.foo<Int>
// expected-swift5-warning@-1 {{cannot explicitly specialize instance method 'foo()'}}
// expected-swift6-error@-2 {{cannot explicitly specialize instance method 'foo()'}}

func foo(_ x: Int) {
_ = {
_ = x<String> // expected-error {{cannot specialize non-generic type 'Int'}}
_ = x<String>
// expected-swift5-error@-1 {{cannot specialize non-generic type 'Int'}}
// expected-swift6-error@-2 {{cannot specialize non-generic type 'Int'}}
}
}

do {
struct Test<T> {
init(_: (T) -> Void) {} // expected-note {{'init(_:)' declared here}}
}

_ = Test.init<Int>({ (_: Int) -> Void in })
// expected-swift5-warning@-1 {{cannot explicitly specialize initializer 'init(_:)'}}
// expected-swift6-error@-2 {{cannot explicitly specialize initializer 'init(_:)'}}
}