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

[SR-11582] Crash when encoding empty decodable class #53987

Open
theblixguy opened this issue Oct 7, 2019 · 8 comments
Open

[SR-11582] Crash when encoding empty decodable class #53987

theblixguy opened this issue Oct 7, 2019 · 8 comments

Comments

@theblixguy
Copy link
Collaborator

@theblixguy theblixguy commented Oct 7, 2019

Previous ID SR-11582
Radar None
Original Reporter @theblixguy
Type Bug
Environment

Swift version 5.1-dev (LLVM 1f54080bb6, Swift ce0c66b1cd)
Target: x86_64-apple-darwin19.0.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Codable, CompilerCrash, DerivedConformance
Assignee bendjones (JIRA)
Priority Medium

md5: f198bfeb40c5eec02635c141a9e3ecf7

Issue Description:

A really simple reproducer:

class Foo: Decodable {}
class Bar: Foo, Encodable {}
Should have found at least 1 var decl
UNREACHABLE executed at /Users/suyashsrijan/Documents/swift-src/swift/lib/Sema/DerivedConformanceCodable.cpp:543!
Stack dump:
0.  Program arguments: ./swiftc -frontend -typecheck /Users/suyashsrijan/Desktop/test.swift
1.  Swift version 5.1-dev (LLVM 1f54080bb6, Swift ce0c66b1cd)
2.  While evaluating request TypeCheckFunctionBodyUntilRequest(test.(file).Bar.encode(to:), )
3.  While type-checking 'encode(to:)' (in module 'test')
4.  While evaluating request ParseAbstractFunctionBodyRequest(test.(file).Bar.encode(to:))
0  swiftc                   0x0000000104fffa55 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swiftc                   0x0000000104ffed05 llvm::sys::RunSignalHandlers() + 85
2  swiftc                   0x0000000105000038 SignalHandler(int) + 264
3  libsystem_platform.dylib 0x00007fff69a23b1d _sigtramp + 29
4  libsystem_platform.dylib 0x0000000000000054 _sigtramp + 2522727764
5  libsystem_c.dylib        0x00007fff698f9a08 abort + 120
6  swiftc                   0x00000001061f503e llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 462
7  swiftc                   0x0000000101dad3b8 lookupVarDeclForCodingKeysCase(swift::DeclContext*, swift::EnumElementDecl*, swift::NominalTypeDecl*) + 456
8  swiftc                   0x0000000101dac4bd deriveBodyEncodable_encode(swift::AbstractFunctionDecl*, void*) + 1213
9  swiftc                   0x00000001020f0a91 swift::ParseAbstractFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 305
10 swiftc                   0x00000001020f0b41 swift::SimpleRequest<swift::ParseAbstractFunctionBodyRequest, swift::BraceStmt* (swift::AbstractFunctionDecl*), (swift::CacheKind)2>::evaluateRequest(swift::ParseAbstractFunctionBodyRequest const&, swift::Evaluator&) + 17
11 swiftc                   0x0000000102241110 llvm::Expected<swift::ParseAbstractFunctionBodyRequest::OutputType> swift::Evaluator::getResultUncached<swift::ParseAbstractFunctionBodyRequest>(swift::ParseAbstractFunctionBodyRequest const&) + 384
12 swiftc                   0x0000000102240e72 llvm::Expected<swift::ParseAbstractFunctionBodyRequest::OutputType> swift::Evaluator::getResultCached<swift::ParseAbstractFunctionBodyRequest, (void*)0>(swift::ParseAbstractFunctionBodyRequest const&) + 66
13 swiftc                   0x000000010224032b llvm::Expected<swift::ParseAbstractFunctionBodyRequest::OutputType> swift::Evaluator::operator()<swift::ParseAbstractFunctionBodyRequest>(swift::ParseAbstractFunctionBodyRequest const&) + 107
14 swiftc                   0x00000001022052f3 swift::ParseAbstractFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::ParseAbstractFunctionBodyRequest>(swift::Evaluator&, swift::ParseAbstractFunctionBodyRequest, swift::ParseAbstractFunctionBodyRequest::OutputType) + 35
15 swiftc                   0x0000000101eaf317 swift::TypeCheckFunctionBodyUntilRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*, swift::SourceLoc) const + 359
16 swiftc                   0x0000000101eacfa5 swift::SimpleRequest<swift::TypeCheckFunctionBodyUntilRequest, bool (swift::AbstractFunctionDecl*, swift::SourceLoc), (swift::CacheKind)1>::evaluateRequest(swift::TypeCheckFunctionBodyUntilRequest const&, swift::Evaluator&) + 21
17 swiftc                   0x0000000101eb7767 llvm::Expected<swift::TypeCheckFunctionBodyUntilRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyUntilRequest>(swift::TypeCheckFunctionBodyUntilRequest const&) + 391
18 swiftc                   0x0000000101eb72a0 llvm::Expected<swift::TypeCheckFunctionBodyUntilRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckFunctionBodyUntilRequest, (void*)0>(swift::TypeCheckFunctionBodyUntilRequest const&) + 240
19 swiftc                   0x0000000101eb65bb llvm::Expected<swift::TypeCheckFunctionBodyUntilRequest::OutputType> swift::Evaluator::operator()<swift::TypeCheckFunctionBodyUntilRequest>(swift::TypeCheckFunctionBodyUntilRequest const&) + 107
20 swiftc                   0x0000000101eafe76 swift::TypeCheckFunctionBodyUntilRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyUntilRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyUntilRequest, swift::TypeCheckFunctionBodyUntilRequest::OutputType) + 38
21 swiftc                   0x0000000101eaf174 swift::TypeChecker::typeCheckAbstractFunctionBody(swift::AbstractFunctionDecl*) + 36
22 swiftc                   0x0000000101edbfc8 typeCheckFunctionsAndExternalDecls(swift::SourceFile&, swift::TypeChecker&) + 312
23 swiftc                   0x0000000101edc9df swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int) + 1775
24 swiftc                   0x0000000101633d39 swift::CompilerInstance::parseAndTypeCheckMainFileUpTo(swift::SourceFile::ASTStage_t, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>) + 489
25 swiftc                   0x00000001016329c7 swift::CompilerInstance::parseAndCheckTypesUpTo(swift::CompilerInstance::ImplicitImports const&, swift::SourceFile::ASTStage_t) + 519
26 swiftc                   0x00000001016321cd swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 621
27 swiftc                   0x000000010141a462 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1650
28 swiftc                   0x0000000101418e86 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3046
29 swiftc                   0x00000001013c2af9 main + 729
30 libdyld.dylib            0x00007fff69822405 start + 1
31 libdyld.dylib            0x0000000000000004 start + 2524830720
zsh: abort      ./swiftc -frontend -typecheck ~/Desktop/test.swift
@theblixguy
Copy link
Collaborator Author

@theblixguy theblixguy commented Oct 7, 2019

The reason this is crashing is because we don't have a stored property named super in Bar.

@theblixguy
Copy link
Collaborator Author

@theblixguy theblixguy commented Oct 7, 2019

I can think of two solutions to this:

  1. We skip the super case in the CodingKeys enum in deriveBodyEncodable_encode

  2. We skip Encodable and/or Decodable synthesis if there are no stored properties in the type i.e. we simply do not derive init(from🙂 and encode(to🙂 methods.

@belkadan Thoughts? Would there be any unwanted side-effects if we do (2)?

@belkadan
Copy link
Contributor

@belkadan belkadan commented Oct 8, 2019

(2) would definitely be source-breaking—consider that Foo also matches that description. I know we've got problems with Codable and classes already, but…

I don't get why we would skip the super case either. Why can't the encoding be empty except for super? That's the most correct thing anyway.

cc bendjones (JIRA User)

@theblixguy
Copy link
Collaborator Author

@theblixguy theblixguy commented Oct 8, 2019

The problem is the compiler is trying to synthesise try container.encode(self.super, forKey: CodingKeys.super) but self.super is not valid.

To clarify, I don't mean we shouldn't synthesise try super.encode(to: container.superEncoder()) - that's valid. But the call above is not, so I think when synthesising container.encode(_:forKey: ), we should skip the super case since we don't have a member named super.

@belkadan
Copy link
Contributor

@belkadan belkadan commented Oct 8, 2019

Looking for a member named "super" would always be wrong, though. It's not specific to this case.

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Oct 9, 2019

Comment by Ben D. Jones (JIRA)

@belkadan I agree with your statement in the above comment. Let me take a look when I can.

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Oct 9, 2019

Comment by Ben D. Jones (JIRA)

@swift-cisync

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Oct 14, 2019

Comment by Ben D. Jones (JIRA)

@swift-ci sync

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants