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-6480] The self value in a class method should always have DynamicSelfType #49030

Open
hamishknight opened this issue Nov 27, 2017 · 6 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-6480
Radar rdar://problem/35730964
Original Reporter @hamishknight
Type Bug
Environment

Swift version 4.1-dev (LLVM fe49d8f2ca, Clang 839070845c, Swift 76ea3a2)
Target: x86_64-apple-darwin17.2.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @slavapestov
Priority Medium

md5: 2df450011114c0bdf1e6f6a7bba3ac45

is duplicated by:

  • SR-14899 self is not convertible to Self type

Issue Description:

The following code outputs "C":

class C {
  static var foo: String { return "\(self)" }
  let bar = foo
}

class D : C {}

print(D().bar) // C

Despite the fact that it's a D instance being constructed.

This also prevents overrides of class members from working:

class C {
  class var foo: String { return "C" }
  let bar = foo
}

class D : C {
  override class var foo: String { return "D" }
}

print(D().bar) // C
@belkadan
Copy link
Contributor

Vaguely related to the lazy property problem. cc @jckarter, @slavapestov

@slavapestov
Copy link
Member

Yes, arguably 'self' should have DynamicSelfType in all class methods, not just those returning self. Right now this cannot be enabled without introducing performance regressions, because of optimizer limitations. I'm going to re-title this bug because finally somebody cares about this issue (yay!). It's a lot more general than just properties.

@slavapestov
Copy link
Member

@swift-ci create

@slavapestov
Copy link
Member

Also arguably, this could use a proposal, since it's source breaking (in a very minor way).

Note that it does not impact ABI, so there's no real urgency to deal with it in the short term.

@belkadan
Copy link
Contributor

I don't think the retitling is correct. If you write a static function that reads foo it does the right thing.

class C {
  static var foo: String { return "\(self)" }
  static func bar() -> String { return foo }
}

class D : C {}

print(D.bar()) // D

@slavapestov
Copy link
Member

@belkadan Hmm. So 'let foo = bar' in type context is type checked like 'let foo = C.bar', not 'let foo = type(of: self).bar'. The latter would be more consistent behavior if we also change methods to have a dynamic 'self' type.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

3 participants