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-9795] Cannot use super in lazy property: 'super' cannot be used outside of class members #52220

Open
marcomasser opened this issue Jan 29, 2019 · 12 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself good first issue Good for newcomers

Comments

@marcomasser
Copy link

Previous ID SR-9795
Radar None
Original Reporter @marcomasser
Type Bug
Environment

Tested with Swift 4.2.1 (Xcode 10.1) and Swift 5 from Xcode 10.2 beta (swiftlang-1001.0.45.7 clang-1001.0.37.7).

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

md5: 75741cacf781b9db21044152b379d272

Issue Description:

I’m not sure this is a bug or if this works as expected:

class Foo {
    var name = "Default Name"
}

class Bar: Foo {
    lazy var fullName: String = {
        return super.name // error: 'super' cannot be used outside of class members
    }()
}

Is there a good reason why super isn’t permitted here? Replacing super with self works fine, as I’d expect because the instance must be fully initialized when lazy properties are accessed.

@belkadan
Copy link
Contributor

Hm, I guess not. @slavapestov, can you think of a reason this would be disallowed after we allowed self?

@slavapestov
Copy link
Contributor

It's just not implemented yet. Should be easy to fix; I suspect there's just an "is self of class method" check somewhere we need to generalize. Tagging as a starter bug

@theblixguy
Copy link
Collaborator

It seems to be happening inside `getImplicitSelfDeclForSuperContext()` because the `getInnermostMethodContext` returns nil (makes sense because the inner most context seems to be an `AbstractClosureExpr`). If you try dumping the decl context, it crashes.

I've sort of got it working by creating an implicit self decl from the innermost type context, but it crashes in CSGen, maybe because of the same reason it crashes in the parser.

@slavapestov
Copy link
Contributor

The lazy initializer is type checked inside an Initializer context. It already has a self decl.

@slavapestov
Copy link
Contributor

See `PatternBindingInitializer::getImplicitSelfDecl()`

@theblixguy
Copy link
Collaborator

Yes, I was trying to creating a `PatternBindingInitializer` earlier but ` getImplicitSelfDecl()` actually causes a crash in `getNumPatternEntries`.

@belkadan
Copy link
Contributor

Where'd you leave off with this one, @theblixguy?

@slavapestov
Copy link
Contributor

Might be worth giving this another shot – the lazy lowering code is a bit cleaner now, and things happen in requests so at least in theory it should be easier to get the ordering dependencies sorted out.

@theblixguy
Copy link
Collaborator

IIRC ` getImplicitSelfDecl` didn't work in that context so had to synthesise one and then tweak the constraint solver, but ended up with a crash in SILGen. I'll give it another try and put up a PR - maybe things have improved now.

@slavapestov
Copy link
Contributor

It shouldn't require synthesizing anything new. 'super' is just a different way of writing 'self' and the fact that it doesn't work in all the places that 'self' works indicates there is some simplification that can be done.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@Overcot
Copy link

Overcot commented May 11, 2022

Hello. Is this issue available to be picked up? Where do i start with it? :)

@ricksentam
Copy link

ricksentam commented May 11, 2022 via email

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 itself good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

6 participants