Skip to content
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

Assertion in typechecker #77924

Open
asl opened this issue Dec 3, 2024 · 2 comments
Open

Assertion in typechecker #77924

asl opened this issue Dec 3, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels type checker Area → compiler: Semantic analysis

Comments

@asl
Copy link
Contributor

asl commented Dec 3, 2024

Description

Consider the attached testcase. The typechecker fails with the assertion:

Assertion failed: (!resolvedType->hasArchetype()), function resolveInterfaceType, file ConstraintSystem.cpp, line 4030.

Reproduction

import _Differentiation

func validate<S, T>(function: @differentiable(reverse) (S) -> (T), with truth: (S) -> (T), at point: S) where S: Differentiable, T: Differentiable, T: FloatingPoint {
    let vwpb = valueWithPullback(at: point, of: function)
    let truthValue = truth(point)
    let gradient = vwpb.pullback(1.0)
    gradient == truthValue
}

Stack dump

0  swift-frontend           0x0000000109e20af4 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109e1f244 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109e21138 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x000000019cff0184 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000019cfbaf70 pthread_kill + 288
5  libsystem_c.dylib        0x000000019cec7908 abort + 128
6  libsystem_c.dylib        0x000000019cec6c1c err + 0
7  swift-frontend           0x0000000109f10650 swift::constraints::Solution::getFunctionArgApplyInfo(swift::constraints::ConstraintLocator*) const (.cold.1) + 0
8  swift-frontend           0x0000000105c77ec0 swift::constraints::Solution::getFunctionArgApplyInfo(swift::constraints::ConstraintLocator*) const + 1192
9  swift-frontend           0x0000000105c0d0e8 swift::constraints::AllowArgumentMismatch::diagnose(swift::constraints::Solution const&, bool) const + 68
10 swift-frontend           0x0000000105b4b0f0 swift::constraints::ConstraintSystem::applySolutionFixes(swift::constraints::Solution const&) + 1272
11 swift-frontend           0x0000000105b4b6f4 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 80
12 swift-frontend           0x0000000105d6c5a0 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 372
13 swift-frontend           0x0000000105d6c3d0 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 196
14 swift-frontend           0x0000000105d6d668 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, swift::Type, swift::PatternBindingDecl*, unsigned int, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 124
15 swift-frontend           0x0000000105d6d8a0 swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, swift::Type, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 248
16 swift-frontend           0x0000000105e42a88 swift::PatternBindingEntryRequest::evaluate(swift::Evaluator&, swift::PatternBindingDecl*, unsigned int) const + 2344
17 swift-frontend           0x000000010613939c swift::PatternBindingEntryRequest::OutputType swift::Evaluator::getResultUncached<swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType)::'lambda'()>(swift::PatternBindingEntryRequest const&, swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType)::'lambda'()) + 212
18 swift-frontend           0x000000010610bfb0 swift::PatternBindingDecl::getCheckedPatternBindingEntry(unsigned int) const + 124
19 swift-frontend           0x0000000105db6780 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 2232
20 swift-frontend           0x0000000105db5eb0 swift::TypeChecker::typeCheckDecl(swift::Decl*) + 116
21 swift-frontend           0x0000000105e3aae8 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 140
22 swift-frontend           0x0000000105e39314 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
23 swift-frontend           0x0000000105e38c14 (anonymous namespace)::StmtChecker::typeCheckBody(swift::BraceStmt*&) + 32
24 swift-frontend           0x0000000105e38914 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1088
25 swift-frontend           0x000000010619fbec swift::TypeCheckFunctionBodyRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()>(swift::TypeCheckFunctionBodyRequest const&, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()) + 224
26 swift-frontend           0x0000000106121dfc swift::AbstractFunctionDecl::getTypecheckedBody() const + 112
27 swift-frontend           0x000000010623dd3c swift::SourceFile::typeCheckDelayedFunctions() + 96
28 swift-frontend           0x0000000105e818d0 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 228
29 swift-frontend           0x0000000105e83288 swift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()>(swift::TypeCheckSourceFileRequest const&, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()) + 216
30 swift-frontend           0x0000000105e817c4 swift::performTypeChecking(swift::SourceFile&) + 84
31 swift-frontend           0x0000000104edfd7c bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_0>(long, swift::SourceFile&) + 16
32 swift-frontend           0x0000000104ed94f8 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 76
33 swift-frontend           0x0000000104ed948c swift::CompilerInstance::performSema() + 76
34 swift-frontend           0x0000000104cb7790 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 60
35 swift-frontend           0x0000000104cacfbc performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 700
36 swift-frontend           0x0000000104cac6dc swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2392
37 swift-frontend           0x0000000104a8e238 swift::mainEntry(int, char const**) + 3052
38 dyld                     0x000000019cc38274 start + 2840

Expected behavior

Invalid program diagnosed properly

Environment

Swift version 6.1-dev (LLVM 42f3e8ef873e24d, Swift 0c8afd3)
Target: arm64-apple-macosx15.0

Additional information

No response

@asl asl added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. type checker Area → compiler: Semantic analysis crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Dec 3, 2024
asl added a commit that referenced this issue Dec 10, 2024
@asl
Copy link
Contributor Author

asl commented Dec 21, 2024

So, the typechecker starts to typecheck the call let gradient = vwpb.pullback(1.0):

---Initial constraints for the given expression---
(call_expr type="S.TangentVector" location=validate.swift:6:25 range=[validate.swift:6:20 - line:6:37] isolation_crossing="none"
  (unresolved_dot_expr type="(T.TangentVector) -> S.TangentVector" location=validate.swift:6:25 range=[validate.swift:6:20 - line:6:25] field="pullback" function_ref=single apply
    (declref_expr type="(value: T, pullback: (T.TangentVector) -> S.TangentVector)" location=validate.swift:6:20 range=[validate.swift:6:20 - line:6:20] decl="validate.(file).validate(function:with:at:)[email protected]:4:9" function_ref=unapplied))
  (argument_list
    (argument
      (float_literal_expr type="$T2" location=validate.swift:6:34 range=[validate.swift:6:34 - line:6:34] value="1.0" builtin_initializer="**NULL**" initializer="**NULL**"))))

And solution is:

---Solution---
Fixed score: [component: applied fix(s), value: 2]
Type variables:
  $T0 as (value: T, pullback: (T.TangentVector) -> S.TangentVector) @ locator@0x13e83a200 [[email protected]:6:20]
  $T1 as (T.TangentVector) -> S.TangentVector @ locator@0x13e83a278 [[email protected]:6:25 → member]
  $T2 as Double @ locator@0x13e83a348 [[email protected]:6:34]
  $T3 as S.TangentVector @ locator@0x13e83a418 [[email protected]:6:25 → function result]
Overload choices:
  locator@0x13e83a200 [[email protected]:6:20] with validate.(file).validate(function:with:at:)[email protected]:4:9 as vwpb: (value: T, pullback: (T.TangentVector) -> S.TangentVector)
  locator@0x13e83a278 [[email protected]:6:25 → member] with tuple (value: T, pullback: (T.TangentVector) -> S.TangentVector) index 1
Trailing closure matching:
  locator@0x13e83a4d8 [[email protected]:6:25 → apply argument]: forward
  locator@0x13e83a250 [[email protected]:6:25 → apply argument]: forward
Fixes:
  [fix: allow argument to parameter type conversion mismatch] @ locator@0x13e83a500 [[email protected]:6:25 → apply argument → comparing call argument #0 to parameter #0]

And it seems that the crash happens inside AllowArgumentMismatch::diagnose. Here from type is:

(struct_type decl="Swift.(file).Double")

and to type is:

(primary_archetype_type address=0x13e4a3ab8 conforms_to="Swift.(file).AdditiveArithmetic" conforms_to="_Differentiation.(file).Differentiable" name="T.TangentVector"
  (interface_type=dependent_member_type assoc_type="_Differentiation.(file).Differentiable.TangentVector"
    (base=generic_type_param_type depth=0 index=1 name="T" param_kind=type)))

So, Solution::resolveInterfaceType cannot handle

(function_type escaping
  (input=function_params num_params=1
    (param
      (primary_archetype_type address=0x13acb34b8 conforms_to="Swift.(file).AdditiveArithmetic" conforms_to="_Differentiation.(file).Differentiable" name="T.TangentVector"
        (interface_type=dependent_member_type assoc_type="_Differentiation.(file).Differentiable.TangentVector"
          (base=generic_type_param_type depth=0 index=1 name="T" param_kind=type)))))
  (output=primary_archetype_type address=0x13acb3440 conforms_to="Swift.(file).AdditiveArithmetic" conforms_to="_Differentiation.(file).Differentiable" name="S.TangentVector"
    (interface_type=dependent_member_type assoc_type="_Differentiation.(file).Differentiable.TangentVector"
      (base=generic_type_param_type depth=0 index=0 name="S" param_kind=type))))

@asl
Copy link
Contributor Author

asl commented Dec 21, 2024

It seems that conformance to FloatingPoint is lost somehow? As then we will have T == T.TangentVector plus corresponding .init.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

1 participant