Skip to content

Commit 45017bf

Browse files
authored
Add tests for calling a base function without qualifications (#6221)
This demonstrates that we don't diagnose when trying to call a base class private function when we refer to the function from a derived class without qualifications. Demo: https://godbolt.org/z/vbWs1P915
1 parent 7050eb8 commit 45017bf

File tree

1 file changed

+106
-10
lines changed

1 file changed

+106
-10
lines changed

toolchain/check/testdata/class/inheritance_access.carbon

Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ library "[[@TEST_NAME]]";
100100

101101
class C {
102102
private fn F() {}
103-
fn G() { Self.F(); }
103+
fn G() {
104+
Self.F();
105+
F();
106+
}
104107
}
105108

106109
// --- noninstance_protected_on_self.carbon
@@ -109,10 +112,13 @@ library "[[@TEST_NAME]]";
109112

110113
class C {
111114
protected fn F() {}
112-
fn G() { Self.F(); }
115+
fn G() {
116+
Self.F();
117+
F();
118+
}
113119
}
114120

115-
// --- fail_noninstance_private_on_parent.carbon
121+
// --- fail_noninstance_private_on_parent_qualified.carbon
116122

117123
library "[[@TEST_NAME]]";
118124

@@ -122,16 +128,29 @@ base class B {
122128

123129
class C {
124130
extend base: B;
125-
// CHECK:STDERR: fail_noninstance_private_on_parent.carbon:[[@LINE+7]]:12: error: cannot access private member `F` of type `B` [ClassInvalidMemberAccess]
131+
// CHECK:STDERR: fail_noninstance_private_on_parent_qualified.carbon:[[@LINE+7]]:12: error: cannot access private member `F` of type `B` [ClassInvalidMemberAccess]
126132
// CHECK:STDERR: fn G() { Self.F(); }
127133
// CHECK:STDERR: ^~~~~~
128-
// CHECK:STDERR: fail_noninstance_private_on_parent.carbon:[[@LINE-8]]:3: note: declared here [ClassMemberDeclaration]
134+
// CHECK:STDERR: fail_noninstance_private_on_parent_qualified.carbon:[[@LINE-8]]:3: note: declared here [ClassMemberDeclaration]
129135
// CHECK:STDERR: private fn F() {}
130136
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
131137
// CHECK:STDERR:
132138
fn G() { Self.F(); }
133139
}
134140

141+
// --- todo_fail_noninstance_private_on_parent_unqualified.carbon
142+
143+
library "[[@TEST_NAME]]";
144+
145+
base class B {
146+
private fn F() {}
147+
}
148+
149+
class C {
150+
extend base: B;
151+
fn G() { F(); }
152+
}
153+
135154
// --- noninstance_protected_on_parent.carbon
136155

137156
library "[[@TEST_NAME]]";
@@ -817,8 +836,10 @@ class B {
817836
// CHECK:STDOUT: fn @C.G() {
818837
// CHECK:STDOUT: !entry:
819838
// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
820-
// CHECK:STDOUT: %F.ref: %C.F.type = name_ref F, @C.%C.F.decl [concrete = constants.%C.F]
821-
// CHECK:STDOUT: %C.F.call: init %empty_tuple.type = call %F.ref()
839+
// CHECK:STDOUT: %F.ref.loc7: %C.F.type = name_ref F, @C.%C.F.decl [concrete = constants.%C.F]
840+
// CHECK:STDOUT: %C.F.call.loc7: init %empty_tuple.type = call %F.ref.loc7()
841+
// CHECK:STDOUT: %F.ref.loc8: %C.F.type = name_ref F, @C.%C.F.decl [concrete = constants.%C.F]
842+
// CHECK:STDOUT: %C.F.call.loc8: init %empty_tuple.type = call %F.ref.loc8()
822843
// CHECK:STDOUT: return
823844
// CHECK:STDOUT: }
824845
// CHECK:STDOUT:
@@ -871,12 +892,14 @@ class B {
871892
// CHECK:STDOUT: fn @C.G() {
872893
// CHECK:STDOUT: !entry:
873894
// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
874-
// CHECK:STDOUT: %F.ref: %C.F.type = name_ref F, @C.%C.F.decl [concrete = constants.%C.F]
875-
// CHECK:STDOUT: %C.F.call: init %empty_tuple.type = call %F.ref()
895+
// CHECK:STDOUT: %F.ref.loc7: %C.F.type = name_ref F, @C.%C.F.decl [concrete = constants.%C.F]
896+
// CHECK:STDOUT: %C.F.call.loc7: init %empty_tuple.type = call %F.ref.loc7()
897+
// CHECK:STDOUT: %F.ref.loc8: %C.F.type = name_ref F, @C.%C.F.decl [concrete = constants.%C.F]
898+
// CHECK:STDOUT: %C.F.call.loc8: init %empty_tuple.type = call %F.ref.loc8()
876899
// CHECK:STDOUT: return
877900
// CHECK:STDOUT: }
878901
// CHECK:STDOUT:
879-
// CHECK:STDOUT: --- fail_noninstance_private_on_parent.carbon
902+
// CHECK:STDOUT: --- fail_noninstance_private_on_parent_qualified.carbon
880903
// CHECK:STDOUT:
881904
// CHECK:STDOUT: constants {
882905
// CHECK:STDOUT: %B: type = class_type @B [concrete]
@@ -948,6 +971,79 @@ class B {
948971
// CHECK:STDOUT: return
949972
// CHECK:STDOUT: }
950973
// CHECK:STDOUT:
974+
// CHECK:STDOUT: --- todo_fail_noninstance_private_on_parent_unqualified.carbon
975+
// CHECK:STDOUT:
976+
// CHECK:STDOUT: constants {
977+
// CHECK:STDOUT: %B: type = class_type @B [concrete]
978+
// CHECK:STDOUT: %B.F.type: type = fn_type @B.F [concrete]
979+
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
980+
// CHECK:STDOUT: %B.F: %B.F.type = struct_value () [concrete]
981+
// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
982+
// CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [concrete]
983+
// CHECK:STDOUT: %C: type = class_type @C [concrete]
984+
// CHECK:STDOUT: %C.elem: type = unbound_element_type %C, %B [concrete]
985+
// CHECK:STDOUT: %C.G.type: type = fn_type @C.G [concrete]
986+
// CHECK:STDOUT: %C.G: %C.G.type = struct_value () [concrete]
987+
// CHECK:STDOUT: %struct_type.base: type = struct_type {.base: %B} [concrete]
988+
// CHECK:STDOUT: %complete_type.98e: <witness> = complete_type_witness %struct_type.base [concrete]
989+
// CHECK:STDOUT: }
990+
// CHECK:STDOUT:
991+
// CHECK:STDOUT: imports {
992+
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
993+
// CHECK:STDOUT: import Core//prelude
994+
// CHECK:STDOUT: import Core//prelude/...
995+
// CHECK:STDOUT: }
996+
// CHECK:STDOUT: }
997+
// CHECK:STDOUT:
998+
// CHECK:STDOUT: file {
999+
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
1000+
// CHECK:STDOUT: .Core = imports.%Core
1001+
// CHECK:STDOUT: .B = %B.decl
1002+
// CHECK:STDOUT: .C = %C.decl
1003+
// CHECK:STDOUT: }
1004+
// CHECK:STDOUT: %Core.import = import Core
1005+
// CHECK:STDOUT: %B.decl: type = class_decl @B [concrete = constants.%B] {} {}
1006+
// CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
1007+
// CHECK:STDOUT: }
1008+
// CHECK:STDOUT:
1009+
// CHECK:STDOUT: class @B {
1010+
// CHECK:STDOUT: %B.F.decl: %B.F.type = fn_decl @B.F [concrete = constants.%B.F] {} {}
1011+
// CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type.357]
1012+
// CHECK:STDOUT: complete_type_witness = %complete_type
1013+
// CHECK:STDOUT:
1014+
// CHECK:STDOUT: !members:
1015+
// CHECK:STDOUT: .Self = constants.%B
1016+
// CHECK:STDOUT: .F [private] = %B.F.decl
1017+
// CHECK:STDOUT: }
1018+
// CHECK:STDOUT:
1019+
// CHECK:STDOUT: class @C {
1020+
// CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [concrete = constants.%B]
1021+
// CHECK:STDOUT: %.loc9: %C.elem = base_decl %B.ref, element0 [concrete]
1022+
// CHECK:STDOUT: %C.G.decl: %C.G.type = fn_decl @C.G [concrete = constants.%C.G] {} {}
1023+
// CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%struct_type.base [concrete = constants.%complete_type.98e]
1024+
// CHECK:STDOUT: complete_type_witness = %complete_type
1025+
// CHECK:STDOUT:
1026+
// CHECK:STDOUT: !members:
1027+
// CHECK:STDOUT: .Self = constants.%C
1028+
// CHECK:STDOUT: .B = <poisoned>
1029+
// CHECK:STDOUT: .base = %.loc9
1030+
// CHECK:STDOUT: .G = %C.G.decl
1031+
// CHECK:STDOUT: .F = <poisoned>
1032+
// CHECK:STDOUT: extend %B.ref
1033+
// CHECK:STDOUT: }
1034+
// CHECK:STDOUT:
1035+
// CHECK:STDOUT: fn @B.F() {
1036+
// CHECK:STDOUT: !entry:
1037+
// CHECK:STDOUT: return
1038+
// CHECK:STDOUT: }
1039+
// CHECK:STDOUT:
1040+
// CHECK:STDOUT: fn @C.G() {
1041+
// CHECK:STDOUT: !entry:
1042+
// CHECK:STDOUT: %F.ref: %B.F.type = name_ref F, @B.%B.F.decl [concrete = constants.%B.F]
1043+
// CHECK:STDOUT: %B.F.call: init %empty_tuple.type = call %F.ref()
1044+
// CHECK:STDOUT: return
1045+
// CHECK:STDOUT: }
1046+
// CHECK:STDOUT:
9511047
// CHECK:STDOUT: --- noninstance_protected_on_parent.carbon
9521048
// CHECK:STDOUT:
9531049
// CHECK:STDOUT: constants {

0 commit comments

Comments
 (0)