Uploaded image for project: 'Swift'
  1. Swift
  2. SR-8172

(T) Reported as Single-Element Tuple Rather Than T

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Won't Do
    • Component/s: Source Tooling
    • Labels:
    • Environment:

      Xcode 10 Beta

      Swift 4.2

      Description

      When the type of a value is declared with organizational parentheses around the type name (or the value), Xcode's Quick Help reports the type as if it is a single-element tuple type.  This reporting is confusing to users.  It gives the impression that single-element tuples are allowed.

      MINIMAL EXAMPLES:

      // Minimal example #1
      let a: (Int) = 42  
      // the explicit type of a is Int;
      // but Xcode reports the type of a as being (Int) 
      
      // Minimal example #2
      let b = (42)
      // the implicit type of b is Int;
      // but Xcode reports the type of b as being (Int) 

      In the examples, above, the values are both of type Int.  The parentheses surrounding Int are meaningless formatting parentheses.  For purposes of representation to the user, the type should be reported as being a plain Int, without the parentheses.  

      BUG:  Xcode's Quick Help reports the type declaration of each as (Int),  a single-element tuple–something that does not currently exist in the Swift language.  See the screenshot, below.

      EVALUATION:  I understand that, under the hood, the compiler utilizes single-element tuples, and generally guards against allowing that implementation detail to leak out to the user.  I believe this bug to be an example of leakage.

      In this case, the bug is (I'm guessing) more an issue of how Xcode populates the Quick Help interface, rather than an issue that can be addressed from the compiler side.  Yet, this bug is rooted in the parser and type checker, where the type is managed as an (Int) rather than an Int.  

      QUESTIONS:  Is it necessary for the compiler to internally utilize single-element tuples?  What is the purpose of an unlabelled single-element tuple?  Would it be preferable for the internal compiler model to better align with the external model implemented by the user?

      MORE DATA:  The post-type-checking state of the AST for Example #2 is set forth, below.

          

      QUICK HELP FOR EXAMPLE #1:

       

      AST FOR EXAMPLE #2:

      let b = (42)

      (source_file

        (top_level_code_decl

          (brace_stmt

            (pattern_binding_decl

              (pattern_named type='(Int)' 'b')

              (paren_expr type='(Int)' location=example.swift:2:10 range=[example.swift:2:9 - line:2:12]

                (call_expr implicit type='Int' location=example.swift:2:10 range=[example.swift:2:10 - line:2:10] nothrow arg_labels=_builtinIntegerLiteral:

                  (constructor_ref_call_expr implicit type='(_MaxBuiltinIntegerType) -> Int' location=example.swift:2:10 range=[example.swift:2:10 - line:2:10] nothrow

                    (declref_expr implicit type='(Int.Type) -> (_MaxBuiltinIntegerType) -> Int' location=example.swift:2:10 range=[example.swift:2:10 - line:2:10] decl=Swift.(file).Int.init(_builtinIntegerLiteral function_ref=single)

                    (type_expr implicit type='Int.Type' location=example.swift:2:10 range=[example.swift:2:10 - line:2:10] typerepr='Int'))

                  (tuple_expr implicit type='(_builtinIntegerLiteral: Int2048)' location=example.swift:2:10 range=[example.swift:2:10 - line:2:10] names=_builtinIntegerLiteral

                    (integer_literal_expr type='Int2048' location=example.swift:2:10 range=[example.swift:2:10 - line:2:10] value=42)))))

      ))

        (var_decl "b" type='(Int)' interface type='(Int)' access=internal let storage_kind=stored))

         

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              Matt Rips Matthew Rips
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: