Skip to content

RangeInfo: disallow expressions with non-void type at the start or the end of multi-statement selections. #8092

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

Merged
merged 1 commit into from
Mar 14, 2017

Conversation

nkcsgexi
Copy link
Contributor

No description provided.

@nkcsgexi
Copy link
Contributor Author

@swift-ci please smoke test

@nkcsgexi nkcsgexi merged commit ed56e49 into swiftlang:master Mar 14, 2017
@nkcsgexi nkcsgexi deleted the range-info-state branch March 14, 2017 23:59
@jrose-apple
Copy link
Contributor

jrose-apple commented Mar 15, 2017

What happens when someone has a call to a @discardableResult function?

@discardableResult func foo() -> Bool { return true }
func test() {
  foo() // non-void type!
}

@nkcsgexi
Copy link
Contributor Author

oh, that's a good point. I think we should allow this as a special case.

@jrose-apple
Copy link
Contributor

Please don't special-case it syntactically. I think you just need a better test for "top-level expression". Maybe it makes more sense to say "they must all be children of the same BraceStmt"?

@jrose-apple
Copy link
Contributor

After all, selecting from outside an if and going only halfway in wouldn't be valid either.

@nkcsgexi
Copy link
Contributor Author

@jrose-apple I think "children of the same BraceStmt" is a better criterion here. I'll rework this patch. 👍

@nkcsgexi
Copy link
Contributor Author

@jrose-apple hmm, actually this approach works well on other cases but not so much inside switch statements, for two reasons: (1) switch statement encloses directly case clauses, no brace stmt is involved; (2) case stmt can also enclose valid multi-statement selection without brace stmt in between.

@jrose-apple
Copy link
Contributor

CaseStmts use BraceStmt too, it's just implicit:

func test(x: Int) {
  switch x {
  case 1:
    1
  default:
    0
  }
}
(source_file
  (func_decl "test(x:)" interface type='(Int) -> ()' access=internal
    (parameter_list
      (parameter "x" apiName=x type='Int' interface type='Int'))
    (brace_stmt
      (switch_stmt
        (declref_expr type='Int' location=<stdin>:2:10 range=[<stdin>:2:10 - line:2:10] decl=main.(file).func decl.x@<stdin>:1:11 function_ref=unapplied specialized=no)
        (case_stmt
          (case_label_item
          (pattern_expr type='Int'
            (call_expr implicit type='Int1' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] nothrow arg_labels=
              (dot_syntax_call_expr implicit type='() -> Int1' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] nothrow
                (declref_expr implicit type='(Bool) -> () -> Int1' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] decl=Swift.(file).Bool._getBuiltinLogicValue() function_ref=double specialized=no)
                (binary_expr implicit type='Bool' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] nothrow
                  (declref_expr implicit type='(Int, Int) -> Bool' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] decl=Swift.(file).~= [with Int[Int: Equatable module Swift]] function_ref=compound specialized=no)
                  (tuple_expr implicit type='(Int, Int)' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8]
                    (call_expr implicit type='Int' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] nothrow arg_labels=_builtinIntegerLiteral:
                      (constructor_ref_call_expr implicit type='(Int2048) -> Int' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] nothrow
                        (declref_expr implicit type='(Int.Type) -> (Int2048) -> Int' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] decl=Swift.(file).Int.init(_builtinIntegerLiteral:) function_ref=single specialized=no)
                        (type_expr implicit type='Int.Type' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] typerepr='Int'))
                      (tuple_expr implicit type='(_builtinIntegerLiteral: Int2048)' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] names=_builtinIntegerLiteral
                        (integer_literal_expr type='Int2048' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] value=1)))
                    (declref_expr implicit type='Int' location=<stdin>:3:8 range=[<stdin>:3:8 - line:3:8] decl=main.(file).func decl.$match@<stdin>:3:8 function_ref=unapplied specialized=no))))
              (tuple_expr implicit type='()'))))
          (brace_stmt
            (call_expr implicit type='Int' location=<stdin>:4:5 range=[<stdin>:4:5 - line:4:5] nothrow arg_labels=_builtinIntegerLiteral:
              (constructor_ref_call_expr implicit type='(Int2048) -> Int' location=<stdin>:4:5 range=[<stdin>:4:5 - line:4:5] nothrow
                (declref_expr implicit type='(Int.Type) -> (Int2048) -> Int' location=<stdin>:4:5 range=[<stdin>:4:5 - line:4:5] decl=Swift.(file).Int.init(_builtinIntegerLiteral:) function_ref=single specialized=no)
                (type_expr implicit type='Int.Type' location=<stdin>:4:5 range=[<stdin>:4:5 - line:4:5] typerepr='Int'))
              (tuple_expr implicit type='(_builtinIntegerLiteral: Int2048)' location=<stdin>:4:5 range=[<stdin>:4:5 - line:4:5] names=_builtinIntegerLiteral
                (integer_literal_expr type='Int2048' location=<stdin>:4:5 range=[<stdin>:4:5 - line:4:5] value=1)))))
        (case_stmt
          (case_label_item
          (pattern_any type='Int'))
          (brace_stmt
            (call_expr implicit type='Int' location=<stdin>:6:5 range=[<stdin>:6:5 - line:6:5] nothrow arg_labels=_builtinIntegerLiteral:
              (constructor_ref_call_expr implicit type='(Int2048) -> Int' location=<stdin>:6:5 range=[<stdin>:6:5 - line:6:5] nothrow
                (declref_expr implicit type='(Int.Type) -> (Int2048) -> Int' location=<stdin>:6:5 range=[<stdin>:6:5 - line:6:5] decl=Swift.(file).Int.init(_builtinIntegerLiteral:) function_ref=single specialized=no)
                (type_expr implicit type='Int.Type' location=<stdin>:6:5 range=[<stdin>:6:5 - line:6:5] typerepr='Int'))
              (tuple_expr implicit type='(_builtinIntegerLiteral: Int2048)' location=<stdin>:6:5 range=[<stdin>:6:5 - line:6:5] names=_builtinIntegerLiteral
                (integer_literal_expr type='Int2048' location=<stdin>:6:5 range=[<stdin>:6:5 - line:6:5] value=0)))))))))

I think we deliberately don't have any other grouping mechanism for statements. (Decls are another story. And top-level code makes this confusing.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants