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

Protocol existentials do not trigger warnings if they will never be executed #73649

Closed
chrisballinger opened this issue May 15, 2024 · 1 comment · Fixed by #73744
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values missing warning Bug: Missing warning SILGen Area → compiler: The SIL generation stage swift 6.0

Comments

@chrisballinger
Copy link

Description

Early returns do not trigger warnings for protocol existentials, see example below

Reproduction

protocol SomeProtocol {
    func doTheThing()
}

func output(someProtocol: SomeProtocol) -> String {
    return "test"
    someProtocol.doTheThing() // this should be a warning
}

Expected behavior

early return should cause a compiler warning on the code that will never be executed

Environment

$ swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

using a generic brings the warning back, e.g.

func output(someProtocol: some SomeProtocol) -> String {
    return "test"
    someProtocol.doTheThing() // this shows a warning
}
@chrisballinger chrisballinger added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels May 15, 2024
@jamieQ
Copy link
Contributor

jamieQ commented May 16, 2024

looked into this a bit out of curiosity. it seems to me likely that this behavior is due to hitting this branch in the SILGen code. it appears that, by default, diagnosing 'implicit' expressions (i.e. those that are not explicitly-written in source code) is suppressed, but there are a handful of carve-outs for things that should be exempted from this rule (e.g. optional chaining). in the reproduction, the relevant portion of the AST is something like:

(open_existential_expr implicit type='()' location=/app/example.swift:12:18 range=[/app/example.swift:12:5 - line:12:29]
  (opaque_value_expr implicit type='any SomeProtocol' location=/app/example.swift:12:5 range=[/app/example.swift:12:5 - line:12:5] @ 0x5a20baa22810)
  (declref_expr type='any SomeProtocol' location=/app/example.swift:12:5 range=[/app/example.swift:12:5 - line:12:5] decl=output.(file).output(someProtocol:maybeSP:someClass:).someProtocol@/app/example.swift:9:13 function_ref=unapplied)
  (call_expr type='()' location=/app/example.swift:12:18 range=[/app/example.swift:12:5 - line:12:29] nothrow isolationCrossing=none
    (dot_syntax_call_expr type='() -> ()' location=/app/example.swift:12:18 range=[/app/example.swift:12:5 - line:12:18] nothrow isolationCrossing=none
      (declref_expr type='(any SomeProtocol) -> () -> ()' location=/app/example.swift:12:18 range=[/app/example.swift:12:18 - line:12:18] decl=output.(file).SomeProtocol.doTheThing()@/app/example.swift:2:10 [with (substitution_map generic_signature=<Self where Self : SomeProtocol> (substitution Self -> @opened("7A730076-1388-11EF-ABE0-81AF1B259855", any SomeProtocol) Self))] function_ref=single)
      (argument_list implicit
        (argument
          (opaque_value_expr implicit type='any SomeProtocol' location=/app/example.swift:12:5 range=[/app/example.swift:12:5 - line:12:5] @ 0x5a20baa22810))))
    (argument_list))))))

so i'd assume adding some special handling for that particular structure (i.e. OpenExistentialExpr), as is done for the optional chaining case, would likely be one way to address the issue. or perhaps there's a way to generalize what the special cases have in common, e.g. a top-level implicit expression which internally contains a non-implicit expression that should be diagnosed.

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation SIL existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values missing warning Bug: Missing warning swift 6.0 SILGen Area → compiler: The SIL generation stage and removed triage needed This issue needs more specific labels SIL labels May 16, 2024
jamieQ added a commit to jamieQ/swift that referenced this issue May 20, 2024
updates unreachable code handling in SILGenStmt.cpp to diagnose
opened existentials that were previously ignored.

resolves: apple#73649
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. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values missing warning Bug: Missing warning SILGen Area → compiler: The SIL generation stage swift 6.0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants