Skip to content

[SR-11857] Swift fails to optimise repeated force unwraps of the same variable. #54266

@Lukasa

Description

@Lukasa
Previous ID SR-11857
Radar rdar://problem/57494816
Original Reporter @Lukasa
Type Bug
Environment

Linux Swift 5.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Performance
Assignee None
Priority Medium

md5: 263a33d97196a85107a6e2fdad176491

Issue Description:

When holding on to an optional local variable, Swift is unable to infer that if one force unwrap of that variable succeeds, all others must succeed as well. There are some potentially invalidating operations that would prevent this optimisation (e.g. calling a mutating function on the Optional), but in simple cases this should be entirely safe to optimise.

Here is a compiler explorer link with an example of the bug in the decodeLoop function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfperformance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions