Skip to content

Commit fa583e3

Browse files
authored
Merge pull request swiftlang#9129 from CodaFi/heyting-arithmetic-by-any-other-name
2 parents c24e1b2 + 253d008 commit fa583e3

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,9 @@ ERROR(extra_trailing_closure_in_call,none,
908908
ERROR(no_accessible_initializers,none,
909909
"%0 cannot be constructed because it has no accessible initializers",
910910
(Type))
911+
ERROR(non_nominal_no_initializers,none,
912+
"non-nominal type %0 does not support explicit initialization",
913+
(Type))
911914
ERROR(unbound_generic_parameter,none,
912915
"generic parameter %0 could not be inferred", (Type))
913916
ERROR(unbound_generic_parameter_cast,none,

lib/Sema/CSDiag.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5279,15 +5279,25 @@ static bool diagnoseSingleCandidateFailures(CalleeCandidateInfo &CCI,
52795279
bool FailureDiagnosis::diagnoseParameterErrors(CalleeCandidateInfo &CCI,
52805280
Expr *fnExpr, Expr *argExpr,
52815281
ArrayRef<Identifier> argLabels) {
5282-
// If we are invoking a constructor and there are absolutely no candidates,
5283-
// then they must all be private.
52845282
if (auto *MTT = fnExpr->getType()->getAs<MetatypeType>()) {
5285-
if (!MTT->getInstanceType()->is<TupleType>() &&
5286-
(CCI.size() == 0 ||
5287-
(CCI.size() == 1 && CCI.candidates[0].getDecl() &&
5288-
isa<ProtocolDecl>(CCI.candidates[0].getDecl())))) {
5289-
CS->TC.diagnose(fnExpr->getLoc(), diag::no_accessible_initializers,
5290-
MTT->getInstanceType());
5283+
auto instTy = MTT->getInstanceType();
5284+
if (instTy->getAnyNominal()) {
5285+
// If we are invoking a constructor on a nominal type and there are
5286+
// absolutely no candidates, then they must all be private.
5287+
if (CCI.size() == 0 || (CCI.size() == 1 && CCI.candidates[0].getDecl() &&
5288+
isa<ProtocolDecl>(CCI.candidates[0].getDecl()))) {
5289+
CS->TC.diagnose(fnExpr->getLoc(), diag::no_accessible_initializers,
5290+
instTy);
5291+
return true;
5292+
}
5293+
// continue below
5294+
} else if (!instTy->is<TupleType>()) {
5295+
// If we are invoking a constructor on a non-nominal type, the expression
5296+
// is malformed.
5297+
SourceRange initExprRange(fnExpr->getSourceRange().Start,
5298+
argExpr->getSourceRange().End);
5299+
CS->TC.diagnose(fnExpr->getLoc(), diag::non_nominal_no_initializers,
5300+
instTy).highlight(initExprRange);
52915301
return true;
52925302
}
52935303
}

test/decl/init/nonnominal_init.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
typealias Not<A> = (A) -> Never
4+
typealias And<A, B> = (left: A, right: B)
5+
indirect enum Or<A, B> {
6+
case inl(A)
7+
case inr(B)
8+
}
9+
10+
func deMorgan<A, B>(_ ne: Not<Or<A, B>>) -> And<Not<A>, Not<B>> {
11+
return And<Not<A>, Not<B>>(
12+
Not<A> { a in ne(.left(a)) }, // expected-error {{non-nominal type '(A) -> Never' does not support explicit initialization}}
13+
Not<B> { a in ne(.right(a)) }
14+
)
15+
}
16+

0 commit comments

Comments
 (0)