Skip to content

[DerivedConformance] Correctly set access of derived 'hash(into:)' #34586

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
Nov 20, 2020

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Nov 5, 2020

If the access level of the target type is private, derived hash(into:) used to be created with private access. But it should be as accessible as the enclosing type.

rdar://problem/71005827

If access of the target type is 'private', derived hash(into:) used to
be created with 'private' access. But it should be 'fileprivate'.

rdar://problem/71005827
@rintaro rintaro force-pushed the ide-completion-rdar71005827 branch from 18711ec to fa7b671 Compare November 5, 2020 03:50
@rintaro
Copy link
Member Author

rintaro commented Nov 5, 2020

Even without this change, the following example is successfully type checked despite the fact E.hash(into:) is synthesized with private.

private enum E {
  case foo, bar
}
private func foo(val: E, hasher: inout Hasher) {
  val.hash(into: &hasher)
}

@xedin Any idea why type checking is not affected by this?

@rintaro
Copy link
Member Author

rintaro commented Nov 5, 2020

@swift-ci Please smoke test

@rintaro rintaro requested a review from slavapestov November 5, 2020 20:00
@xedin
Copy link
Contributor

xedin commented Nov 18, 2020

I wonder if that's because first lookup for hash(into:) returns just a stub for a method which would be generated later?

@xedin
Copy link
Contributor

xedin commented Nov 18, 2020

Looks like type-checker finds a method on a Hashable protocol which has public access:

(func_decl "hash(into:)" interface type='<Self where Self : Hashable> (Self) -> (inout Hasher) -> ()' access=public
  (parameter "self")
  (parameter_list
    (parameter "hasher" apiName=into type='Hasher' interface type='Hasher' inout)))

and uses it with Self of E although the witness would have private access. Why doesn't this create problems down the line, @CodaFi?

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me!

@rintaro
Copy link
Member Author

rintaro commented Nov 20, 2020

Apparently the mandatory SIL optimizer correctly(?) resolves and rewrites it to a direct call to E.hash(into:).

-emit-silgen

  %7 = witness_method $E, #Hashable.hash : <Self where Self : Hashable> (Self) -> (inout Hasher) -> () : $@convention(witness_method: Hashable) <τ_0_0 where τ_0_0 : Hashable> (@inout Hasher, @in_guaranteed τ_0_0) -> () // user: %8
  %8 = apply %7<E>(%6, %4) : $@convention(witness_method: Hashable) <τ_0_0 where τ_0_0 : Hashable> (@inout Hasher, @in_guaranteed τ_0_0) -> ()

-emit-sil

  %5 = function_ref @$s4test1E33_83378C430F65473055F1BD53F3ADCDB7LLO4hash4intoys6HasherVz_tF : $@convention(method) (@inout Hasher, E) -> () // user: %6
  %6 = apply %5(%4, %0) : $@convention(method) (@inout Hasher, E) -> ()

Anyway 🚢

@rintaro rintaro merged commit e066d43 into swiftlang:main Nov 20, 2020
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