Skip to content

Calling friend function of type nested in C++ namespace crashes Swift compiler #83085

@maddyadams

Description

@maddyadams

Description

No response

Reproduction

// .h
namespace MyNS {
    struct Foo;
    
    Foo makeFoo();
    
    struct Foo {
    private:
        Foo() = default;
        friend Foo makeFoo();
    };
}

// .cpp
MyNS::Foo MyNS::makeFoo() {
    return Foo();
}
let x: MyNS.Foo = MyNS.makeFoo()

Stack dump

1.	Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
2.	Compiling with effective version 5.10
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/maddyadams/Desktop/ForwardDeclaredNoncopyableCrash/ForwardDeclaredNoncopyableCrash/main.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(ForwardDeclaredNoncopyableCrash.(file).foo()@/Users/maddyadams/Desktop/ForwardDeclaredNoncopyableCrash/ForwardDeclaredNoncopyableCrash/main.swift:12:6)
5.	While type-checking statement at [/Users/maddyadams/Desktop/ForwardDeclaredNoncopyableCrash/ForwardDeclaredNoncopyableCrash/main.swift:12:12 - line:14:1] RangeText="{
    let x: MyNS.Foo = MyNS.makeFoo()
"
6.	While type-checking declaration 0x151d4d558 (at /Users/maddyadams/Desktop/ForwardDeclaredNoncopyableCrash/ForwardDeclaredNoncopyableCrash/main.swift:13:5)
7.	While type-checking expression at [/Users/maddyadams/Desktop/ForwardDeclaredNoncopyableCrash/ForwardDeclaredNoncopyableCrash/main.swift:13:23 - line:13:36] RangeText="MyNS.makeFoo("
8.	While type-checking-target starting at /Users/maddyadams/Desktop/ForwardDeclaredNoncopyableCrash/ForwardDeclaredNoncopyableCrash/main.swift:13:28
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x000000010ada2c28 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010ada0a60 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010ada3264 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x000000018090b624 _sigtramp + 56
4  swift-frontend           0x0000000105f41970 swift::constraints::ConstraintSystem::getTypeOfMemberReference(swift::Type, swift::ValueDecl*, swift::DeclContext*, bool, swift::FunctionRefKind, swift::constraints::ConstraintLocator*, llvm::DenseMap<swift::GenericTypeParamType*, swift::TypeVariableType*, llvm::DenseMapInfo<swift::GenericTypeParamType*, void>, llvm::detail::DenseMapPair<swift::GenericTypeParamType*, swift::TypeVariableType*>>*) + 3016
5  swift-frontend           0x0000000105f4392c swift::constraints::ConstraintSystem::resolveOverload(swift::constraints::ConstraintLocator*, swift::Type, swift::constraints::OverloadChoice, swift::DeclContext*) + 760
6  swift-frontend           0x0000000105dd31cc swift::constraints::ConstraintSystem::simplifyConstraint(swift::constraints::Constraint const&) + 1856
7  swift-frontend           0x0000000105dbed48 swift::constraints::ConstraintSystem::simplifyMemberConstraint(swift::constraints::ConstraintKind, swift::Type, swift::DeclNameRef, swift::Type, swift::DeclContext*, swift::FunctionRefKind, llvm::ArrayRef<swift::constraints::OverloadChoice>, swift::optionset::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 4320
8  swift-frontend           0x0000000105d86294 (anonymous namespace)::ConstraintGenerator::addMemberRefConstraints(swift::Expr*, swift::Expr*, swift::DeclNameRef, swift::FunctionRefKind, llvm::ArrayRef<swift::ValueDecl*>) + 760
9  swift-frontend           0x0000000105d7cf44 (anonymous namespace)::ConstraintGenerator::visitUnresolvedDotExpr(swift::UnresolvedDotExpr*) + 584
10 swift-frontend           0x0000000105d776c8 (anonymous namespace)::ConstraintWalker::walkToExprPost(swift::Expr*) + 1584
11 swift-frontend           0x00000001063be854 (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) + 116
12 swift-frontend           0x00000001063bbdf0 (anonymous namespace)::Traversal::visit(swift::Expr*) + 84
13 swift-frontend           0x0000000105d7114c swift::constraints::ConstraintSystem::generateConstraints(swift::Expr*, swift::DeclContext*) + 216
14 swift-frontend           0x0000000105d7084c swift::constraints::ConstraintSystem::generateConstraints(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 1812
15 swift-frontend           0x0000000105dea03c swift::constraints::ConstraintSystem::solve(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 1112
16 swift-frontend           0x0000000105ff7914 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 704
17 swift-frontend           0x0000000105ff7530 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) + 420
18 swift-frontend           0x0000000105ff8d14 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, swift::Type, swift::PatternBindingDecl*, unsigned int, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 200
19 swift-frontend           0x0000000105ff8f30 swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, swift::Type, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 292
20 swift-frontend           0x0000000106054014 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 5408
21 swift-frontend           0x00000001060f7174 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 792
22 swift-frontend           0x00000001060fca64 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 288
23 swift-frontend           0x00000001060fa3fc bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 312
24 swift-frontend           0x00000001060f89cc swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1488
25 swift-frontend           0x00000001064f55ec 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'()) + 640
26 swift-frontend           0x00000001064581a8 swift::AbstractFunctionDecl::getTypecheckedBody() const + 160
27 swift-frontend           0x000000010614a7e0 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 912
28 swift-frontend           0x0000000106150b18 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'()) + 624
29 swift-frontend           0x000000010614a434 swift::performTypeChecking(swift::SourceFile&) + 308
30 swift-frontend           0x0000000104fb4018 swift::CompilerInstance::performSema() + 248
31 swift-frontend           0x0000000104bf7274 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 772
32 swift-frontend           0x0000000104bf5fd8 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3716
33 swift-frontend           0x0000000104b7a0bc swift::mainEntry(int, char const**) + 5428
34 dyld                     0x0000000180532b4c start + 6000

Expected behavior

I expected that the code would compile successfully. (Note that in a .cpp file, the equivalent MyNS::Foo x = MyNS::makeFoo(); compiles without issue)

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

If Foo is not nested in a namespace, the crash does not occur.

rdar://147000056

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.c++ interopFeature: Interoperability with C++crashBug: A crash, i.e., an abnormal termination of software

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions