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-626] No diagnostic for unconditional recursion #43243

Closed
swift-ci opened this issue Jan 27, 2016 · 11 comments
Closed

[SR-626] No diagnostic for unconditional recursion #43243

swift-ci opened this issue Jan 27, 2016 · 11 comments
Assignees
Labels
compiler The Swift compiler in itself improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-626
Radar None
Original Reporter eyelash (JIRA User)
Type Improvement
Status Resolved
Resolution Done
Environment

Swift version 3.0-dev (LLVM f95d47afa7, Clang f66c5bb67b, Swift b745691)
Target: x86_64-unknown-linux-gnu
on Ubuntu 15.10

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee @CodaFi
Priority Medium

md5: 3d750f414400561a5ad5d932a92d4914

is duplicated by:

  • SR-6219 ~= Validation
  • SR-6451 Initializer is able to recursively call itself with no warnings

relates to:

  • SR-3016 Infinite recursion without side-effects can be optimized to undef
  • SR-2559 EXC_BAD_ACCESS when comparing two optionals
  • SR-5224 Unable to detect infinite recursion defining lazy property
  • SR-677 Infinite recursion instead of compiler error - overwritten function
  • SR-4406 Unable to infer correct overload in constructor leads to infinite recursion

Issue Description:

The following code compiles without errors but executing the created binary results in a Segfault:

extension Array: Equatable {}
public func ==<Element>(lhs: Array<Element>, rhs: Array<Element>) -> Bool {
    return lhs == rhs
}

func checkEquals<T: Equatable>(_ x: T, _ y: T) {
    if (x == y) {
        print("equals")
    }
}

checkEquals([1, 2], [1, 2])
@belkadan
Copy link
Contributor

cc @DougGregor

@natecook1000
Copy link
Member

This is recursing infinitely, so it crashes when it runs out of stack space, just like

func f() { f() }
f()

@belkadan
Copy link
Contributor

Ah, so this is fully correct type-checking-wise, but it would be nice™ to warn on unconditional recursion, like Clang does.

@colemancda
Copy link
Contributor

I think maybe the warning should be different depending on the case.

  • For methods or functions, there might be a case where recursion is necessary, so a warning on unconditional recursion should be shown.
  • However, for initializers there is no scenario where calling the same initializer (not super) will not crash, so that should be a build error, IMHO.

@belkadan
Copy link
Contributor

That's not necessarily true; you could pass different arguments to it.

@colemancda
Copy link
Contributor

@belkadan In that case it would be a convenience initializer, I mean a normal non-convenience initializer.

@belkadan
Copy link
Contributor

Oh, good point. Designated initializers can't delegate. Okay, I'll un-dup your original bug.

@belkadan
Copy link
Contributor

No, wait, a struct initializer doesn't have to be marked convenience to do it.

@colemancda
Copy link
Contributor

In that case, the compiler should throw an error for non-convenience class initializers. It may be a small thing, be it makes the language safer. I had a property that I initialized in a class before init() (e.g. var property = Foo()). I spent 2 days debugging this in my code and tracking it down, since it would crash before arriving at init() it was hard to track. It may be a simple mistake, but in large codebases can lead to hard to track bugs.

@belkadan
Copy link
Contributor

It already does that:

class Test {
  init() {
    self.init()
  }
}
<stdin>:2:3: error: designated initializer for 'Test' cannot delegate (with 'self.init'); did you mean this to be a convenience initializer?
  init() {
  ^
  convenience 
<stdin>:3:10: note: delegation occurs here
    self.init()
         ^

@CodaFi
Copy link
Member

CodaFi commented Feb 27, 2018

Resolved by the merge of #11869.

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

No branches or pull requests

5 participants