Skip to content

Commit 0343bb1

Browse files
authored
Revert "[Sema] Add specialization constraints for func and variable types, then diagnose w/fixes."
1 parent 6fd0ddc commit 0343bb1

12 files changed

+76
-164
lines changed

Diff for: include/swift/AST/DiagnosticsSema.def

+2
Original file line numberDiff line numberDiff line change
@@ -4605,6 +4605,8 @@ NOTE(duplicated_key_declared_here, none,
46054605
// Generic specializations
46064606
ERROR(cannot_explicitly_specialize_generic_function,none,
46074607
"cannot explicitly specialize a generic function", ())
4608+
ERROR(not_a_generic_definition,none,
4609+
"cannot specialize a non-generic definition", ())
46084610
ERROR(not_a_generic_type,none,
46094611
"cannot specialize non-generic type %0", (Type))
46104612
ERROR(protocol_declares_unknown_primary_assoc_type,none,

Diff for: include/swift/Sema/CSFix.h

+1-31
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,9 @@ enum class FixKind : uint8_t {
462462
/// because its name doesn't appear in 'initializes' or 'accesses' attributes.
463463
AllowInvalidMemberReferenceInInitAccessor,
464464

465-
/// Ignore an attempt to specialize a non-generic type.
465+
/// Ignore an attempt to specialize non-generic type.
466466
AllowConcreteTypeSpecialization,
467467

468-
/// Ignore an attempt to specialize a generic function.
469-
AllowGenericFunctionSpecialization,
470-
471468
/// Ignore an out-of-place \c then statement.
472469
IgnoreOutOfPlaceThenStmt,
473470

@@ -3746,33 +3743,6 @@ class AllowConcreteTypeSpecialization final : public ConstraintFix {
37463743
}
37473744
};
37483745

3749-
class AllowGenericFunctionSpecialization final : public ConstraintFix {
3750-
ValueDecl *Decl;
3751-
3752-
AllowGenericFunctionSpecialization(ConstraintSystem &cs, ValueDecl *decl,
3753-
ConstraintLocator *locator)
3754-
: ConstraintFix(cs, FixKind::AllowConcreteTypeSpecialization, locator),
3755-
Decl(decl) {}
3756-
3757-
public:
3758-
std::string getName() const override {
3759-
return "allow generic function specialization";
3760-
}
3761-
3762-
bool diagnose(const Solution &solution, bool asNote = false) const override;
3763-
3764-
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
3765-
return diagnose(*commonFixes.front().first);
3766-
}
3767-
3768-
static AllowGenericFunctionSpecialization *
3769-
create(ConstraintSystem &cs, ValueDecl *decl, ConstraintLocator *locator);
3770-
3771-
static bool classof(const ConstraintFix *fix) {
3772-
return fix->getKind() == FixKind::AllowGenericFunctionSpecialization;
3773-
}
3774-
};
3775-
37763746
class IgnoreOutOfPlaceThenStmt final : public ConstraintFix {
37773747
IgnoreOutOfPlaceThenStmt(ConstraintSystem &cs, ConstraintLocator *locator)
37783748
: ConstraintFix(cs, FixKind::IgnoreOutOfPlaceThenStmt, locator) {}

Diff for: lib/Parse/ParseType.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -1476,9 +1476,7 @@ static bool isGenericTypeDisambiguatingToken(Parser &P) {
14761476
auto &tok = P.Tok;
14771477
switch (tok.getKind()) {
14781478
default:
1479-
// If this is the end of the expr (wouldn't match parseExprSequenceElement),
1480-
// prefer generic type list over an illegal unary postfix '>' operator.
1481-
return P.isStartOfSwiftDecl() || P.isStartOfStmt(/*prefer expr=*/true);
1479+
return false;
14821480
case tok::r_paren:
14831481
case tok::r_square:
14841482
case tok::l_brace:

Diff for: lib/Sema/CSDiagnostics.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -9360,12 +9360,7 @@ bool InvalidMemberReferenceWithinInitAccessor::diagnoseAsError() {
93609360
}
93619361

93629362
bool ConcreteTypeSpecialization::diagnoseAsError() {
9363-
emitDiagnostic(diag::not_a_generic_type, resolveType(ConcreteType));
9364-
return true;
9365-
}
9366-
9367-
bool GenericFunctionSpecialization::diagnoseAsError() {
9368-
emitDiagnostic(diag::cannot_explicitly_specialize_generic_function);
9363+
emitDiagnostic(diag::not_a_generic_type, ConcreteType);
93699364
return true;
93709365
}
93719366

Diff for: lib/Sema/CSDiagnostics.h

+1-12
Original file line numberDiff line numberDiff line change
@@ -3132,18 +3132,7 @@ class ConcreteTypeSpecialization final : public FailureDiagnostic {
31323132
ConcreteTypeSpecialization(const Solution &solution, Type concreteTy,
31333133
ConstraintLocator *locator)
31343134
: FailureDiagnostic(solution, locator),
3135-
ConcreteType(concreteTy) {}
3136-
3137-
bool diagnoseAsError() override;
3138-
};
3139-
3140-
class GenericFunctionSpecialization final : public FailureDiagnostic {
3141-
ValueDecl *Decl;
3142-
3143-
public:
3144-
GenericFunctionSpecialization(const Solution &solution, ValueDecl *decl,
3145-
ConstraintLocator *locator)
3146-
: FailureDiagnostic(solution, locator), Decl(decl) {}
3135+
ConcreteType(resolveType(concreteTy)) {}
31473136

31483137
bool diagnoseAsError() override;
31493138
};

Diff for: lib/Sema/CSFix.cpp

-12
Original file line numberDiff line numberDiff line change
@@ -2616,18 +2616,6 @@ AllowConcreteTypeSpecialization::create(ConstraintSystem &cs, Type concreteTy,
26162616
AllowConcreteTypeSpecialization(cs, concreteTy, locator);
26172617
}
26182618

2619-
bool AllowGenericFunctionSpecialization::diagnose(const Solution &solution,
2620-
bool asNote) const {
2621-
GenericFunctionSpecialization failure(solution, Decl, getLocator());
2622-
return failure.diagnose(asNote);
2623-
}
2624-
2625-
AllowGenericFunctionSpecialization *AllowGenericFunctionSpecialization::create(
2626-
ConstraintSystem &cs, ValueDecl *decl, ConstraintLocator *locator) {
2627-
return new (cs.getAllocator())
2628-
AllowGenericFunctionSpecialization(cs, decl, locator);
2629-
}
2630-
26312619
bool IgnoreOutOfPlaceThenStmt::diagnose(const Solution &solution,
26322620
bool asNote) const {
26332621
OutOfPlaceThenStmtFailure failure(solution, getLocator());

Diff for: lib/Sema/CSGen.cpp

+50-24
Original file line numberDiff line numberDiff line change
@@ -1903,9 +1903,9 @@ namespace {
19031903
/// introduce them as an explicit generic arguments constraint.
19041904
///
19051905
/// \returns true if resolving any of the specialization types failed.
1906-
void addSpecializationConstraint(ConstraintLocator *locator, Type boundType,
1907-
SourceLoc lAngleLoc,
1908-
ArrayRef<TypeRepr *> specializationArgs) {
1906+
bool addSpecializationConstraint(
1907+
ConstraintLocator *locator, Type boundType,
1908+
ArrayRef<TypeRepr *> specializationArgs) {
19091909
// Resolve each type.
19101910
SmallVector<Type, 2> specializationArgTypes;
19111911
auto options =
@@ -1916,36 +1916,61 @@ namespace {
19161916
options |= TypeResolutionFlags::AllowPackReferences;
19171917
elementEnv = OuterExpansions.back();
19181918
}
1919-
auto result = TypeResolution::resolveContextualType(
1919+
const auto result = TypeResolution::resolveContextualType(
19201920
specializationArg, CurDC, options,
19211921
// Introduce type variables for unbound generics.
19221922
OpenUnboundGenericType(CS, locator),
19231923
HandlePlaceholderType(CS, locator),
19241924
OpenPackElementType(CS, locator, elementEnv));
1925-
if (result->hasError()) {
1926-
auto &ctxt = CS.getASTContext();
1927-
auto *repr = new (ctxt) PlaceholderTypeRepr(specializationArg->getLoc());
1928-
result = PlaceholderType::get(ctxt, repr);
1929-
ctxt.Diags.diagnose(lAngleLoc,
1930-
diag::while_parsing_as_left_angle_bracket);
1931-
}
1925+
if (result->hasError())
1926+
return true;
1927+
19321928
specializationArgTypes.push_back(result);
19331929
}
19341930

1935-
auto constraint = Constraint::create(
1936-
CS, ConstraintKind::ExplicitGenericArguments, boundType,
1937-
PackType::get(CS.getASTContext(), specializationArgTypes), locator);
1938-
CS.addUnsolvedConstraint(constraint);
1939-
CS.activateConstraint(constraint);
1931+
CS.addConstraint(
1932+
ConstraintKind::ExplicitGenericArguments, boundType,
1933+
PackType::get(CS.getASTContext(), specializationArgTypes),
1934+
locator);
1935+
return false;
19401936
}
19411937

19421938
Type visitUnresolvedSpecializeExpr(UnresolvedSpecializeExpr *expr) {
19431939
auto baseTy = CS.getType(expr->getSubExpr());
1944-
auto *overloadLocator = CS.getConstraintLocator(expr->getSubExpr());
1945-
addSpecializationConstraint(
1946-
overloadLocator, baseTy->getMetatypeInstanceType(),
1947-
expr->getLAngleLoc(), expr->getUnresolvedParams());
1948-
return baseTy;
1940+
1941+
if (baseTy->isTypeVariableOrMember()) {
1942+
return baseTy;
1943+
}
1944+
1945+
// We currently only support explicit specialization of generic types.
1946+
// FIXME: We could support explicit function specialization.
1947+
auto &de = CS.getASTContext().Diags;
1948+
if (baseTy->is<AnyFunctionType>()) {
1949+
de.diagnose(expr->getSubExpr()->getLoc(),
1950+
diag::cannot_explicitly_specialize_generic_function);
1951+
de.diagnose(expr->getLAngleLoc(),
1952+
diag::while_parsing_as_left_angle_bracket);
1953+
return Type();
1954+
}
1955+
1956+
if (AnyMetatypeType *meta = baseTy->getAs<AnyMetatypeType>()) {
1957+
auto *overloadLocator = CS.getConstraintLocator(expr->getSubExpr());
1958+
if (addSpecializationConstraint(overloadLocator,
1959+
meta->getInstanceType(),
1960+
expr->getUnresolvedParams())) {
1961+
return Type();
1962+
}
1963+
1964+
return baseTy;
1965+
}
1966+
1967+
// FIXME: If the base type is a type variable, constrain it to a metatype
1968+
// of a bound generic type.
1969+
de.diagnose(expr->getSubExpr()->getLoc(),
1970+
diag::not_a_generic_definition);
1971+
de.diagnose(expr->getLAngleLoc(),
1972+
diag::while_parsing_as_left_angle_bracket);
1973+
return Type();
19491974
}
19501975

19511976
Type visitSequenceExpr(SequenceExpr *expr) {
@@ -4055,9 +4080,10 @@ namespace {
40554080

40564081
// Add explicit generic arguments, if there were any.
40574082
if (expr->getGenericArgsRange().isValid()) {
4058-
addSpecializationConstraint(CS.getConstraintLocator(expr), macroRefType,
4059-
expr->getGenericArgsRange().Start,
4060-
expr->getGenericArgs());
4083+
if (addSpecializationConstraint(
4084+
CS.getConstraintLocator(expr), macroRefType,
4085+
expr->getGenericArgs()))
4086+
return Type();
40614087
}
40624088

40634089
// Form the applicable-function constraint. The result type

Diff for: lib/Sema/CSSimplify.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -13950,8 +13950,6 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
1395013950

1395113951
// Bail out if we haven't selected an overload yet.
1395213952
auto simplifiedBoundType = simplifyType(type1, flags);
13953-
if (simplifiedBoundType->isPlaceholder())
13954-
return SolutionKind::Solved;
1395513953
if (simplifiedBoundType->isTypeVariableOrMember())
1395613954
return formUnsolved();
1395713955

@@ -14044,12 +14042,13 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
1404414042
}
1404514043
}
1404614044

14045+
if (!decl->getAsGenericContext())
14046+
return SolutionKind::Error;
14047+
1404714048
auto genericParams = getGenericParams(decl);
14048-
if (!decl->getAsGenericContext() || !genericParams) {
14049-
return recordFix(AllowConcreteTypeSpecialization::create(
14050-
*this, type1, getConstraintLocator(locator)))
14051-
? SolutionKind::Error
14052-
: SolutionKind::Solved;
14049+
if (!genericParams) {
14050+
// FIXME: Record an error here that we're ignoring the parameters.
14051+
return SolutionKind::Solved;
1405314052
}
1405414053

1405514054
// Map the generic parameters we have over to their opened types.
@@ -14082,14 +14081,12 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
1408214081
}
1408314082
}
1408414083

14085-
// FIXME: We could support explicit function specialization.
14086-
if (openedGenericParams.empty() ||
14087-
(isa<AbstractFunctionDecl>(decl) && !hasParameterPack)) {
14084+
if (openedGenericParams.empty()) {
1408814085
if (!shouldAttemptFixes())
1408914086
return SolutionKind::Error;
1409014087

14091-
return recordFix(AllowGenericFunctionSpecialization::create(
14092-
*this, decl, getConstraintLocator(locator)))
14088+
return recordFix(AllowConcreteTypeSpecialization::create(
14089+
*this, type1, getConstraintLocator(locator)))
1409314090
? SolutionKind::Error
1409414091
: SolutionKind::Solved;
1409514092
}
@@ -15220,7 +15217,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1522015217
case FixKind::AllowAssociatedValueMismatch:
1522115218
case FixKind::GenericArgumentsMismatch:
1522215219
case FixKind::AllowConcreteTypeSpecialization:
15223-
case FixKind::AllowGenericFunctionSpecialization:
1522415220
case FixKind::IgnoreGenericSpecializationArityMismatch:
1522515221
case FixKind::IgnoreKeyPathSubscriptIndexMismatch:
1522615222
case FixKind::AllowMemberRefOnExistential: {

Diff for: test/Constraints/ambiguous_specialized_name_diagnostics.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func test() {
4242

4343
S<Int>(t: 42).test() // expected-error {{ambiguous use of 'init(t:)'}}
4444

45+
// FIXME(diagnostics): This should produce ambiguity diagnostic too
4546
S<Int>.staticFn()
46-
// expected-error@-1 {{ambiguous use of 'staticFn()'}}
47+
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
4748
}

Diff for: test/Parse/enum_element_pattern_swift4.swift

+4-10
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ enum E {
99

1010
static func testE(e: E) {
1111
switch e {
12-
case A<UndefinedTy>(): // expected-error {{cannot find type 'UndefinedTy' in scope}}
12+
case A<UndefinedTy>(): // expected-error {{cannot specialize a non-generic definition}}
1313
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
14-
// expected-error@-2 {{cannot specialize non-generic type 'E'}}
15-
// expected-error@-3 {{cannot call value of non-function type 'E'}}
1614
break
17-
case B<Int>(): // expected-error {{cannot specialize non-generic type 'E'}}
18-
// expected-error@-1 {{cannot call value of non-function type 'E'}}
15+
case B<Int>(): // expected-error {{cannot specialize a non-generic definition}} expected-note {{while parsing this '<' as a type parameter bracket}}
1916
break
2017
default:
2118
break;
@@ -25,13 +22,10 @@ enum E {
2522

2623
func testE(e: E) {
2724
switch e {
28-
case E.A<UndefinedTy>(): // expected-error {{cannot find type 'UndefinedTy' in scope}}
25+
case E.A<UndefinedTy>(): // expected-error {{cannot specialize a non-generic definition}}
2926
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
30-
// expected-error@-2 {{cannot specialize non-generic type 'E'}}
31-
// expected-error@-3 {{cannot call value of non-function type 'E'}}
3227
break
33-
case E.B<Int>(): // expected-error {{cannot specialize non-generic type 'E'}}
34-
// expected-error@-1 {{cannot call value of non-function type 'E'}}
28+
case E.B<Int>(): // expected-error {{cannot specialize a non-generic definition}} expected-note {{while parsing this '<' as a type parameter bracket}}
3529
break
3630
case .C(): // expected-error {{pattern with associated values does not match enum case 'C'}}
3731
// expected-note@-1 {{remove associated values to make the pattern match}} {{10-12=}}

Diff for: test/Parse/generic_disambiguation.swift

+5-11
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,15 @@ var a, b, c, d : Int
2626
_ = a < b
2727
_ = (a < b, c > d)
2828
// Parses as generic because of lparen after '>'
29-
(a < b, c > (d)) // expected-error{{cannot find type 'b' in scope}}
30-
// expected-note@-1 2 {{while parsing this '<' as a type parameter bracket}}
31-
// expected-error@-2 {{cannot specialize non-generic type 'Int'}}
32-
// expected-error@-3 {{cannot call value of non-function type 'Int'}}
33-
// expected-error@-4 {{cannot find type 'c' in scope}}
29+
(a < b, c > (d)) // expected-error{{cannot specialize a non-generic definition}}
30+
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
3431
// Parses as generic because of lparen after '>'
35-
(a<b, c>(d)) // expected-error{{cannot find type 'b' in scope}}
36-
// expected-note@-1 2 {{while parsing this '<' as a type parameter bracket}}
37-
// expected-error@-2 {{cannot specialize non-generic type 'Int'}}
38-
// expected-error@-3 {{cannot call value of non-function type 'Int'}}
39-
// expected-error@-4 {{cannot find type 'c' in scope}}
32+
(a<b, c>(d)) // expected-error{{cannot specialize a non-generic definition}}
33+
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
4034
_ = a>(b)
4135
_ = a > (b)
4236

43-
generic<Int>(0) // expected-error{{cannot explicitly specialize a generic function}}
37+
generic<Int>(0) // expected-error{{cannot explicitly specialize a generic function}} expected-note{{while parsing this '<' as a type parameter bracket}}
4438

4539
A<B>.c()
4640
A<A<B>>.c()

Diff for: test/Sema/generic-arg-list.swift

-41
This file was deleted.

0 commit comments

Comments
 (0)