Skip to content

Commit d688710

Browse files
committed
[Diag] Move invalid where clause on top-level decl diagnostic to Sema
1 parent ca686a5 commit d688710

File tree

6 files changed

+27
-26
lines changed

6 files changed

+27
-26
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,9 +1658,6 @@ ERROR(redundant_class_requirement,none,
16581658
"redundant 'class' requirement", ())
16591659
ERROR(late_class_requirement,none,
16601660
"'class' must come first in the requirement list", ())
1661-
ERROR(where_toplevel_nongeneric,none,
1662-
"'where' clause cannot be attached to non-generic "
1663-
"top-level declaration", ())
16641661
ERROR(where_inside_brackets,none,
16651662
"'where' clause next to generic parameters is obsolete, "
16661663
"must be written following the declaration's type", ())

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ NOTE(unstable_mangled_name_add_objc,none,
15751575
"for compatibility with existing archives, use '@objc' "
15761576
"to record the Swift 3 runtime name", ())
15771577

1578-
// Generic types
1578+
// Generic declarations
15791579
ERROR(unsupported_type_nested_in_generic_function,none,
15801580
"type %0 cannot be nested in generic function %1",
15811581
(Identifier, DeclName))
@@ -1591,6 +1591,12 @@ ERROR(unsupported_type_nested_in_protocol_extension,none,
15911591
ERROR(unsupported_nested_protocol,none,
15921592
"protocol %0 cannot be nested inside another declaration",
15931593
(Identifier))
1594+
ERROR(where_nongeneric_ctx,none,
1595+
"'where' clause on non-generic member declaration requires a "
1596+
"generic context", ())
1597+
ERROR(where_nongeneric_toplevel,none,
1598+
"'where' clause cannot be applied to a non-generic top-level "
1599+
"declaration", ())
15941600

15951601
// Type aliases
15961602
ERROR(type_alias_underlying_type_access,none,
@@ -2755,10 +2761,6 @@ ERROR(dynamic_self_stored_property_init,none,
27552761
ERROR(dynamic_self_default_arg,none,
27562762
"covariant 'Self' type cannot be referenced from a default argument expression", ())
27572763

2758-
ERROR(where_nongeneric_ctx,none,
2759-
"'where' clause on non-generic member declaration requires a "
2760-
"generic context", ())
2761-
27622764
//------------------------------------------------------------------------------
27632765
// MARK: Type Check Attributes
27642766
//------------------------------------------------------------------------------

lib/Parse/ParseGeneric.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,10 @@ parseFreestandingGenericWhereClause(GenericContext *genCtx,
418418

419419
genericParams->addTrailingWhereClause(Context, WhereLoc, Requirements);
420420

421-
// A where clause that references only outer generic parameters?
422-
} else if (flags.contains(PD_HasContainerType)) {
421+
} else {
422+
// A where clause against outer generic parameters.
423423
genCtx->setTrailingWhereClause(
424424
TrailingWhereClause::create(Context, WhereLoc, Requirements));
425-
} else {
426-
diagnose(WhereLoc, diag::where_toplevel_nongeneric);
427425
}
428426

429427
return ParserStatus();

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,15 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
620620
return cast<SubscriptDecl>(accessor->getStorage())->getGenericSignature();
621621
}
622622

623-
// ...or we may have a where clause dependent on outer generic parameters.
623+
// ...or we may only have a contextual where clause.
624624
} else if (const auto *where = GC->getTrailingWhereClause()) {
625625
// If there is no generic context for the where clause to
626626
// rely on, diagnose that now and bail out.
627-
if (!GC->isGenericContext()) {
627+
if (GC->getParent()->isModuleScopeContext()) {
628+
GC->getASTContext().Diags.diagnose(where->getWhereLoc(),
629+
diag::where_nongeneric_toplevel);
630+
return nullptr;
631+
} else if (!GC->isGenericContext()) {
628632
GC->getASTContext().Diags.diagnose(where->getWhereLoc(),
629633
diag::where_nongeneric_ctx);
630634
return nullptr;

test/Generics/invalid.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
func bet() where A : B {} // expected-error {{'where' clause cannot be attached to non-generic top-level declaration}}
4-
5-
typealias gimel = Int where A : B // expected-error {{'where' clause cannot be attached to non-generic top-level declaration}}
6-
7-
class dalet where A : B {} // expected-error {{'where' clause cannot be attached to non-generic top-level declaration}}
8-
9-
struct Where {
10-
func bet() where A == B {} // expected-error {{'where' clause on non-generic member declaration requires a generic context}}
11-
typealias gimel = Int where A : B // expected-error {{'where' clause on non-generic member declaration requires a generic context}}
12-
class dalet where A : B {} // expected-error {{'where' clause on non-generic member declaration requires a generic context}}
13-
}
14-
153
protocol he where A : B { // expected-error {{use of undeclared type 'A'}}
164
// expected-error@-1 {{use of undeclared type 'B'}}
175

test/Generics/where_clause_contextually_generic_decls.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// RUN: %target-typecheck-verify-swift -typecheck %s -verify -swift-version 4
22

3+
func bet() where A : B {} // expected-error {{'where' clause cannot be applied to a non-generic top-level declaration}}
4+
5+
typealias gimel = Int where A : B // expected-error {{'where' clause cannot be applied to a non-generic top-level declaration}}
6+
7+
class dalet where A : B {} // expected-error {{'where' clause cannot be applied to a non-generic top-level declaration}}
8+
9+
struct Where {
10+
func bet() where A == B {} // expected-error {{'where' clause on non-generic member declaration requires a generic context}}
11+
typealias gimel = Int where A : B // expected-error {{'where' clause on non-generic member declaration requires a generic context}}
12+
class dalet where A : B {} // expected-error {{'where' clause on non-generic member declaration requires a generic context}}
13+
}
14+
315
// Make sure Self: ... is correctly diagnosed in classes
416

517
class SelfInGenericClass<T> {

0 commit comments

Comments
 (0)