-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Sema] Add specialization constraints for func and variable types, then diagnose w/fixes. #74909
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1903,9 +1903,9 @@ namespace { | |
| /// introduce them as an explicit generic arguments constraint. | ||
| /// | ||
| /// \returns true if resolving any of the specialization types failed. | ||
| bool addSpecializationConstraint( | ||
| ConstraintLocator *locator, Type boundType, | ||
| ArrayRef<TypeRepr *> specializationArgs) { | ||
| void addSpecializationConstraint(ConstraintLocator *locator, Type boundType, | ||
| SourceLoc lAngleLoc, | ||
| ArrayRef<TypeRepr *> specializationArgs) { | ||
| // Resolve each type. | ||
| SmallVector<Type, 2> specializationArgTypes; | ||
| auto options = | ||
|
|
@@ -1916,61 +1916,36 @@ namespace { | |
| options |= TypeResolutionFlags::AllowPackReferences; | ||
| elementEnv = OuterExpansions.back(); | ||
| } | ||
| const auto result = TypeResolution::resolveContextualType( | ||
| auto result = TypeResolution::resolveContextualType( | ||
| specializationArg, CurDC, options, | ||
| // Introduce type variables for unbound generics. | ||
| OpenUnboundGenericType(CS, locator), | ||
| HandlePlaceholderType(CS, locator), | ||
| OpenPackElementType(CS, locator, elementEnv)); | ||
| if (result->hasError()) | ||
| return true; | ||
|
|
||
| if (result->hasError()) { | ||
| auto &ctxt = CS.getASTContext(); | ||
| auto *repr = new (ctxt) PlaceholderTypeRepr(specializationArg->getLoc()); | ||
| result = PlaceholderType::get(ctxt, repr); | ||
|
||
| ctxt.Diags.diagnose(lAngleLoc, | ||
| diag::while_parsing_as_left_angle_bracket); | ||
| } | ||
| specializationArgTypes.push_back(result); | ||
| } | ||
|
|
||
| CS.addConstraint( | ||
| ConstraintKind::ExplicitGenericArguments, boundType, | ||
| PackType::get(CS.getASTContext(), specializationArgTypes), | ||
| locator); | ||
| return false; | ||
| auto constraint = Constraint::create( | ||
| CS, ConstraintKind::ExplicitGenericArguments, boundType, | ||
| PackType::get(CS.getASTContext(), specializationArgTypes), locator); | ||
| CS.addUnsolvedConstraint(constraint); | ||
| CS.activateConstraint(constraint); | ||
| } | ||
|
|
||
| Type visitUnresolvedSpecializeExpr(UnresolvedSpecializeExpr *expr) { | ||
| auto baseTy = CS.getType(expr->getSubExpr()); | ||
|
|
||
| if (baseTy->isTypeVariableOrMember()) { | ||
| return baseTy; | ||
| } | ||
|
|
||
| // We currently only support explicit specialization of generic types. | ||
| // FIXME: We could support explicit function specialization. | ||
| auto &de = CS.getASTContext().Diags; | ||
| if (baseTy->is<AnyFunctionType>()) { | ||
| de.diagnose(expr->getSubExpr()->getLoc(), | ||
| diag::cannot_explicitly_specialize_generic_function); | ||
| de.diagnose(expr->getLAngleLoc(), | ||
| diag::while_parsing_as_left_angle_bracket); | ||
| return Type(); | ||
| } | ||
|
|
||
| if (AnyMetatypeType *meta = baseTy->getAs<AnyMetatypeType>()) { | ||
| auto *overloadLocator = CS.getConstraintLocator(expr->getSubExpr()); | ||
| if (addSpecializationConstraint(overloadLocator, | ||
| meta->getInstanceType(), | ||
| expr->getUnresolvedParams())) { | ||
| return Type(); | ||
| } | ||
|
|
||
| return baseTy; | ||
| } | ||
|
|
||
| // FIXME: If the base type is a type variable, constrain it to a metatype | ||
| // of a bound generic type. | ||
| de.diagnose(expr->getSubExpr()->getLoc(), | ||
| diag::not_a_generic_definition); | ||
| de.diagnose(expr->getLAngleLoc(), | ||
| diag::while_parsing_as_left_angle_bracket); | ||
| return Type(); | ||
| auto *overloadLocator = CS.getConstraintLocator(expr->getSubExpr()); | ||
| addSpecializationConstraint( | ||
| overloadLocator, baseTy->getMetatypeInstanceType(), | ||
| expr->getLAngleLoc(), expr->getUnresolvedParams()); | ||
| return baseTy; | ||
| } | ||
|
|
||
| Type visitSequenceExpr(SequenceExpr *expr) { | ||
|
|
@@ -4082,10 +4057,9 @@ namespace { | |
|
|
||
| // Add explicit generic arguments, if there were any. | ||
| if (expr->getGenericArgsRange().isValid()) { | ||
| if (addSpecializationConstraint( | ||
| CS.getConstraintLocator(expr), macroRefType, | ||
| expr->getGenericArgs())) | ||
| return Type(); | ||
| addSpecializationConstraint(CS.getConstraintLocator(expr), macroRefType, | ||
| expr->getGenericArgsRange().Start, | ||
| expr->getGenericArgs()); | ||
gregomni marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Form the applicable-function constraint. The result type | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // RUN: %target-typecheck-verify-swift | ||
|
|
||
| extension Int { | ||
| func foo() -> Int {} | ||
| var bar: Int { | ||
| get {} | ||
| } | ||
|
|
||
| func baz() -> Int {} | ||
| func baz(_ x: Int = 0) -> Int {} | ||
|
|
||
| func gen<T>() -> T {} // expected-note 2 {{in call to function 'gen()'}} | ||
| } | ||
|
|
||
| // 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.gen<Int>() // expected-error {{cannot explicitly specialize a generic function}} | ||
| // expected-error@-1 {{generic parameter 'T' could not be inferred}} | ||
|
|
||
| let _ = 0.foo<Int>() // expected-error {{cannot specialize non-generic type '() -> Int'}} | ||
|
|
||
| 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'}} | ||
| } | ||
|
|
||
| extension Bool { | ||
| func foo<T>() -> T {} | ||
| } | ||
|
|
||
| let _: () -> Bool = false.foo<Int> // expected-error {{cannot explicitly specialize a generic function}} | ||
|
|
||
| func foo(_ x: Int) { | ||
| _ = { | ||
| _ = x<String> // expected-error {{cannot specialize non-generic type 'Int'}} | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'd prefer the resolveType happened during construction of the diagnostic, it's usually safer that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. That was an accidental leftover from the changes along the way.