Skip to content

Sema: Fix crash in type resolution when tuple type contains type variables #78893

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 2 commits into from
Jan 25, 2025
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
14 changes: 5 additions & 9 deletions lib/AST/Requirement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,24 @@ checkRequirementsImpl(ArrayRef<Requirement> requirements,
while (!worklist.empty()) {
auto req = worklist.pop_back_val();

// Check preconditions.
#ifndef NDEBUG
{
// Check preconditions.
auto firstType = req.getFirstType();
assert((allowTypeParameters || !firstType->hasTypeParameter())
ASSERT((allowTypeParameters || !firstType->hasTypeParameter())
&& "must take a contextual type. if you really are ok with an "
"indefinite answer (and usually YOU ARE NOT), then consider whether "
"you really, definitely are ok with an indefinite answer, and "
"use `checkRequirementsWithoutContext` instead");
assert(!firstType->hasTypeVariable());
ASSERT(!firstType->hasTypeVariable());

if (req.getKind() != RequirementKind::Layout) {
auto secondType = req.getSecondType();
assert((allowTypeParameters || !secondType->hasTypeParameter())
ASSERT((allowTypeParameters || !secondType->hasTypeParameter())
&& "must take a contextual type. if you really are ok with an "
"indefinite answer (and usually YOU ARE NOT), then consider whether "
"you really, definitely are ok with an indefinite answer, and "
"use `checkRequirementsWithoutContext` instead");
assert(!secondType->hasTypeVariable());
ASSERT(!secondType->hasTypeVariable());
}
}
#endif

switch (req.checkRequirement(worklist, /*allowMissing=*/true)) {
case CheckRequirementResult::Success:
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5676,6 +5676,7 @@ NeverNullType TypeResolver::resolveTupleType(TupleTypeRepr *repr,
inStage(TypeResolutionStage::Interface) &&
!moveOnlyElementIndex.has_value() &&
!ty->hasUnboundGenericType() &&
!ty->hasTypeVariable() &&
!isa<TupleTypeRepr>(tyR)) {
auto contextTy = GenericEnvironment::mapTypeIntoContext(
resolution.getGenericSignature().getGenericEnvironment(), ty);
Expand Down
5 changes: 5 additions & 0 deletions test/Constraints/issue-77315.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: %target-typecheck-verify-swift

func foo<T>(_: () -> (Optional<T>, Int)) -> T { fatalError() }

let x: Int = foo { () -> (Optional, Int) in fatalError() }