Skip to content

Error when one associated type is constrained to another. #10053

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 2, 2017
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
16 changes: 13 additions & 3 deletions lib/Sema/ITCDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,24 @@ void IterativeTypeChecker::processResolveInheritedClauseEntry(

// Validate the type of this inherited clause entry.
// FIXME: Recursion into existing type checker.
GenericTypeToArchetypeResolver resolver(dc);
if (TC.validateType(*inherited, dc, options, &resolver,
Optional<ProtocolRequirementTypeResolver> protoResolver;
Optional<GenericTypeToArchetypeResolver> archetypeResolver;
GenericTypeResolver *resolver;
if (auto *proto = dyn_cast<ProtocolDecl>(dc)) {
protoResolver.emplace(proto);
resolver = protoResolver.getPointer();
} else {
archetypeResolver.emplace(dc);
resolver = archetypeResolver.getPointer();
}

if (TC.validateType(*inherited, dc, options, resolver,
&unsatisfiedDependency)) {
inherited->setInvalidType(getASTContext());
}

auto type = inherited->getType();
if (!type.isNull())
if (!type.isNull() && !isa<ProtocolDecl>(dc))
inherited->setType(dc->mapTypeOutOfContext(type));
}

Expand Down
7 changes: 4 additions & 3 deletions test/Sema/circular_decl_checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func TopLevelGenericFunc2<T : TopLevelGenericFunc2>(x: T) -> T { return x} // ex
var TopLevelVar: TopLevelVar? { return nil } // expected-error 2 {{use of undeclared type 'TopLevelVar'}}


protocol AProtocol {
// FIXME: Should produce an error here, but it's currently causing problems.
associatedtype e : e
// FIXME: The first error is redundant, isn't correct in what it states, and
// also should be emitted on the inheritance clause.
protocol AProtocol { // expected-error {{first type 'Self.e' in conformance requirement does not refer to a generic parameter or associated type}}
associatedtype e : e // expected-error {{inheritance from non-protocol, non-class type 'Self.e'}}
}


Expand Down
3 changes: 2 additions & 1 deletion test/decl/nested/protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ class OuterClass {

class OtherGenericClass<T> {
protocol InnerProtocol : OtherGenericClass { }
// expected-error@-1{{protocol 'InnerProtocol' cannot be nested inside another declaration}}
// expected-error@-1{{non-class type 'InnerProtocol' cannot inherit from class 'OtherGenericClass<T>'}}
// expected-error@-2{{protocol 'InnerProtocol' cannot be nested inside another declaration}}
}
18 changes: 18 additions & 0 deletions test/decl/protocol/req/unsatisfiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,21 @@ protocol P3 {
func f<T: P3>(_: T) where T.A == Self.A, T.A: C // expected-error{{instance method requirement 'f' cannot add constraint 'Self.A: C' on 'Self'}}
func g<T: P3>(_: T) where T.A: C, T.A == Self.A // expected-error{{instance method requirement 'g' cannot add constraint 'Self.A: C' on 'Self'}}
}

protocol Base {
associatedtype Assoc
}

// FIXME: The first error is redundant, isn't correct in what it states, and
// also should be emitted on the inheritance clause.
// FIXME: This used to /not/ error in Swift 3. It didn't impose any statically-
// enforced requirements, but the compiler crashed if you used anything but the
// same type.
protocol Sub1: Base { // expected-error {{first type 'Self.Assoc' in conformance requirement does not refer to a generic parameter or associated type}}
associatedtype SubAssoc: Assoc // expected-error {{inheritance from non-protocol, non-class type 'Self.Assoc'}}
}
// FIXME: This error is incorrect in what it states and should be emitted on
// the where-clause.
protocol Sub2: Base { // expected-error {{first type 'Self.Assoc' in conformance requirement does not refer to a generic parameter or associated type}}
associatedtype SubAssoc where SubAssoc: Assoc
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
protocol b:Self