mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 19:40:10 +01:00
Allow witness tables to support aliases and other types of StructValue (#7738)
Changing the check from "is `FunctionDecl`" to "has `FunctionType`" provides us with more flexibilty to use aliases and other types of `StructValue`.
This commit is contained in:
@@ -50,9 +50,8 @@ auto CheckAssociatedFunctionImplementation(
|
||||
Context& context, SemIR::FunctionType interface_function_type,
|
||||
SemIR::SpecificId enclosing_specific_id, SemIR::InstId impl_decl_id,
|
||||
bool defer_thunk_definition) -> SemIR::InstId {
|
||||
auto impl_function_decl =
|
||||
context.insts().TryGetAs<SemIR::FunctionDecl>(impl_decl_id);
|
||||
if (!impl_function_decl) {
|
||||
auto impl_function_decl = context.insts().Get(impl_decl_id).type_id();
|
||||
if (!context.types().Is<SemIR::FunctionType>(impl_function_decl)) {
|
||||
if (impl_decl_id != SemIR::ErrorInst::InstId) {
|
||||
CARBON_DIAGNOSTIC(ImplFunctionWithNonFunction, Error,
|
||||
"associated function {0} implemented by non-function",
|
||||
|
||||
+243
-309
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,120 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon
|
||||
//
|
||||
// AUTOUPDATE
|
||||
// TIP: To test this file alone, run:
|
||||
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/impl_assoc_fn_as_alias.carbon
|
||||
// TIP: To dump output, run:
|
||||
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/impl_assoc_fn_as_alias.carbon
|
||||
|
||||
//@dump-sem-ir-begin
|
||||
interface I { fn F(); }
|
||||
|
||||
fn PossiblyF();
|
||||
|
||||
class FAlias {
|
||||
impl as I {
|
||||
alias F = PossiblyF;
|
||||
}
|
||||
}
|
||||
//@dump-sem-ir-end
|
||||
|
||||
// CHECK:STDOUT: --- impl_assoc_fn_as_alias.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: constants {
|
||||
// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
|
||||
// CHECK:STDOUT: %Self: %I.type = symbolic_binding Self, 0 [symbolic]
|
||||
// CHECK:STDOUT: %I.WithSelf.F.type.dcc: type = fn_type @I.WithSelf.F, @I.WithSelf(%Self) [symbolic]
|
||||
// CHECK:STDOUT: %I.WithSelf.F.ec6: %I.WithSelf.F.type.dcc = struct_value () [symbolic]
|
||||
// CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete]
|
||||
// CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, @I.WithSelf.%I.WithSelf.F.decl [concrete]
|
||||
// CHECK:STDOUT: %PossiblyF.type: type = fn_type @PossiblyF [concrete]
|
||||
// CHECK:STDOUT: %PossiblyF: %PossiblyF.type = struct_value () [concrete]
|
||||
// CHECK:STDOUT: %FAlias: type = class_type @FAlias [concrete]
|
||||
// CHECK:STDOUT: %.a11: <witness> = impl_self_witness %FAlias, @I [concrete]
|
||||
// CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness @FAlias.as.I.impl.%I.impl_witness_table [concrete]
|
||||
// CHECK:STDOUT: %I.facet: %I.type = facet_value %FAlias, (%I.impl_witness) [concrete]
|
||||
// CHECK:STDOUT: %I.WithSelf.F.type.962: type = fn_type @I.WithSelf.F, @I.WithSelf(%I.facet) [concrete]
|
||||
// CHECK:STDOUT: %I.WithSelf.F.85d: %I.WithSelf.F.type.962 = struct_value () [concrete]
|
||||
// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
|
||||
// CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: %I.decl: type = interface_decl @I [concrete = constants.%I.type] {} {}
|
||||
// CHECK:STDOUT: %PossiblyF.decl: %PossiblyF.type = fn_decl @PossiblyF [concrete = constants.%PossiblyF] {} {}
|
||||
// CHECK:STDOUT: %FAlias.decl: type = class_decl @FAlias [concrete = constants.%FAlias] {} {}
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: interface @I {
|
||||
// CHECK:STDOUT: %Self: %I.type = symbolic_binding Self, 0 [symbolic = constants.%Self]
|
||||
// CHECK:STDOUT: %I.WithSelf.decl = interface_with_self_decl @I [concrete]
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !with Self:
|
||||
// CHECK:STDOUT: %I.WithSelf.F.decl: @I.WithSelf.%I.WithSelf.F.type (%I.WithSelf.F.type.dcc) = fn_decl @I.WithSelf.F [symbolic = @I.WithSelf.%I.WithSelf.F (constants.%I.WithSelf.F.ec6)] {} {}
|
||||
// CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, %I.WithSelf.F.decl [concrete = constants.%assoc0]
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !members:
|
||||
// CHECK:STDOUT: .Self = %Self
|
||||
// CHECK:STDOUT: .F = @I.WithSelf.%assoc0
|
||||
// CHECK:STDOUT: .PossiblyF = <poisoned>
|
||||
// CHECK:STDOUT: witness = (@I.WithSelf.%I.WithSelf.F.decl)
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !requires:
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: impl @FAlias.as.I.impl: %Self.ref as %I.ref {
|
||||
// CHECK:STDOUT: %PossiblyF.ref: %PossiblyF.type = name_ref PossiblyF, file.%PossiblyF.decl [concrete = constants.%PossiblyF]
|
||||
// CHECK:STDOUT: %F: %PossiblyF.type = alias_binding F, %PossiblyF.ref [concrete = constants.%PossiblyF]
|
||||
// CHECK:STDOUT: %I.impl_witness_table = impl_witness_table (%F), @FAlias.as.I.impl [concrete]
|
||||
// CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness %I.impl_witness_table [concrete = constants.%I.impl_witness]
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !members:
|
||||
// CHECK:STDOUT: .PossiblyF = <poisoned>
|
||||
// CHECK:STDOUT: .F = %F
|
||||
// CHECK:STDOUT: extend %I.ref
|
||||
// CHECK:STDOUT: witness = %I.impl_witness
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: class @FAlias {
|
||||
// CHECK:STDOUT: impl_decl @FAlias.as.I.impl [concrete] {} {
|
||||
// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%FAlias [concrete = constants.%FAlias]
|
||||
// CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %.loc19: <witness> = impl_self_witness @FAlias.as.I.impl.%Self.ref, @I [concrete = constants.%.a11]
|
||||
// CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
|
||||
// CHECK:STDOUT: complete_type_witness = %complete_type
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !members:
|
||||
// CHECK:STDOUT: .Self = constants.%FAlias
|
||||
// CHECK:STDOUT: .I = <poisoned>
|
||||
// CHECK:STDOUT: .PossiblyF = <poisoned>
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: generic fn @I.WithSelf.F(@I.%Self: %I.type) {
|
||||
// CHECK:STDOUT: fn();
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @PossiblyF();
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: specific @I.WithSelf(constants.%Self) {
|
||||
// CHECK:STDOUT: !definition:
|
||||
// CHECK:STDOUT: %Self => constants.%Self
|
||||
// CHECK:STDOUT: %I.WithSelf.F.type => constants.%I.WithSelf.F.type.dcc
|
||||
// CHECK:STDOUT: %I.WithSelf.F => constants.%I.WithSelf.F.ec6
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: specific @I.WithSelf.F(constants.%Self) {}
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: specific @I.WithSelf(constants.%I.facet) {
|
||||
// CHECK:STDOUT: !definition:
|
||||
// CHECK:STDOUT: %Self => constants.%I.facet
|
||||
// CHECK:STDOUT: %I.WithSelf.F.type => constants.%I.WithSelf.F.type.962
|
||||
// CHECK:STDOUT: %I.WithSelf.F => constants.%I.WithSelf.F.85d
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: specific @I.WithSelf.F(constants.%I.facet) {}
|
||||
// CHECK:STDOUT:
|
||||
+98
-12
@@ -10,6 +10,10 @@
|
||||
// TIP: To dump output, run:
|
||||
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/impl/assoc_fn_alias.carbon
|
||||
|
||||
// --- class_method_defined_as_alias.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
interface I {
|
||||
fn F(self) -> i32;
|
||||
}
|
||||
@@ -29,16 +33,41 @@ fn Call(a: A) -> i32 {
|
||||
return a.F();
|
||||
}
|
||||
|
||||
// --- impl_method_defined_as_alias.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
interface I {
|
||||
fn F(self) -> i32;
|
||||
}
|
||||
|
||||
class C {
|
||||
var i: i32;
|
||||
fn F(self) -> i32 { return self.i; }
|
||||
}
|
||||
|
||||
impl C as I {
|
||||
alias F = C.F;
|
||||
}
|
||||
|
||||
fn Call[I: I](i: I) -> i32 {
|
||||
return i.F();
|
||||
}
|
||||
|
||||
fn Call2(c: C) {
|
||||
Call(c);
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: ; ---
|
||||
// CHECK:STDOUT: ; ModuleID = 'assoc_fn_alias.carbon'
|
||||
// CHECK:STDOUT: source_filename = "assoc_fn_alias.carbon"
|
||||
// CHECK:STDOUT: ; ModuleID = 'class_method_defined_as_alias.carbon'
|
||||
// CHECK:STDOUT: source_filename = "class_method_defined_as_alias.carbon"
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define i32 @"_CF.A.Main:I.Main"(ptr %self) #0 !dbg !4 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: %.loc24_16.1.n = getelementptr inbounds nuw { i32 }, ptr %self, i32 0, i32 0, !dbg !11
|
||||
// CHECK:STDOUT: %.loc24_16.2 = load i32, ptr %.loc24_16.1.n, align 4, !dbg !11
|
||||
// CHECK:STDOUT: ret i32 %.loc24_16.2, !dbg !12
|
||||
// CHECK:STDOUT: %.loc15_16.1.n = getelementptr inbounds nuw { i32 }, ptr %self, i32 0, i32 0, !dbg !11
|
||||
// CHECK:STDOUT: %.loc15_16.2 = load i32, ptr %.loc15_16.1.n, align 4, !dbg !11
|
||||
// CHECK:STDOUT: ret i32 %.loc15_16.2, !dbg !12
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
@@ -54,21 +83,78 @@ fn Call(a: A) -> i32 {
|
||||
// CHECK:STDOUT: !llvm.module.flags = !{!2, !3}
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
||||
// CHECK:STDOUT: !1 = !DIFile(filename: "assoc_fn_alias.carbon", directory: "")
|
||||
// CHECK:STDOUT: !1 = !DIFile(filename: "class_method_defined_as_alias.carbon", directory: "")
|
||||
// CHECK:STDOUT: !2 = !{i32 7, !"Dwarf Version", i32 5}
|
||||
// CHECK:STDOUT: !3 = !{i32 2, !"Debug Info Version", i32 3}
|
||||
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.A.Main:I.Main", scope: null, file: !1, line: 23, type: !5, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
|
||||
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.A.Main:I.Main", scope: null, file: !1, line: 14, type: !5, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
|
||||
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
|
||||
// CHECK:STDOUT: !6 = !{!7, !8}
|
||||
// CHECK:STDOUT: !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
|
||||
// CHECK:STDOUT: !8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
|
||||
// CHECK:STDOUT: !9 = !{!10}
|
||||
// CHECK:STDOUT: !10 = !DILocalVariable(arg: 1, scope: !4, type: !8)
|
||||
// CHECK:STDOUT: !11 = !DILocation(line: 24, column: 12, scope: !4)
|
||||
// CHECK:STDOUT: !12 = !DILocation(line: 24, column: 5, scope: !4)
|
||||
// CHECK:STDOUT: !13 = distinct !DISubprogram(name: "Call", linkageName: "_CCall.Main", scope: null, file: !1, line: 28, type: !5, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14)
|
||||
// CHECK:STDOUT: !11 = !DILocation(line: 15, column: 12, scope: !4)
|
||||
// CHECK:STDOUT: !12 = !DILocation(line: 15, column: 5, scope: !4)
|
||||
// CHECK:STDOUT: !13 = distinct !DISubprogram(name: "Call", linkageName: "_CCall.Main", scope: null, file: !1, line: 19, type: !5, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14)
|
||||
// CHECK:STDOUT: !14 = !{!15}
|
||||
// CHECK:STDOUT: !15 = !DILocalVariable(arg: 1, scope: !13, type: !8)
|
||||
// CHECK:STDOUT: !16 = !DILocation(line: 29, column: 10, scope: !13)
|
||||
// CHECK:STDOUT: !17 = !DILocation(line: 29, column: 3, scope: !13)
|
||||
// CHECK:STDOUT: !16 = !DILocation(line: 20, column: 10, scope: !13)
|
||||
// CHECK:STDOUT: !17 = !DILocation(line: 20, column: 3, scope: !13)
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; ---
|
||||
// CHECK:STDOUT: ; ModuleID = 'impl_method_defined_as_alias.carbon'
|
||||
// CHECK:STDOUT: source_filename = "impl_method_defined_as_alias.carbon"
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define i32 @_CF.C.Main(ptr %self) #0 !dbg !4 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: %.loc10_34.1.i = getelementptr inbounds nuw { i32 }, ptr %self, i32 0, i32 0, !dbg !11
|
||||
// CHECK:STDOUT: %.loc10_34.2 = load i32, ptr %.loc10_34.1.i, align 4, !dbg !11
|
||||
// CHECK:STDOUT: ret i32 %.loc10_34.2, !dbg !12
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define void @_CCall2.Main(ptr %c) #0 !dbg !13 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: %Call.call = call i32 @_CCall.Main.0676fa98bb7dbd66(ptr %c), !dbg !18
|
||||
// CHECK:STDOUT: ret void, !dbg !19
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define linkonce_odr i32 @_CCall.Main.0676fa98bb7dbd66(ptr %i) #0 !dbg !20 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: %I.WithSelf.F.call = call i32 @_CF.C.Main(ptr %i), !dbg !23
|
||||
// CHECK:STDOUT: ret i32 %I.WithSelf.F.call, !dbg !24
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: attributes #0 = { nounwind }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !llvm.dbg.cu = !{!0}
|
||||
// CHECK:STDOUT: !llvm.module.flags = !{!2, !3}
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
||||
// CHECK:STDOUT: !1 = !DIFile(filename: "impl_method_defined_as_alias.carbon", directory: "")
|
||||
// CHECK:STDOUT: !2 = !{i32 7, !"Dwarf Version", i32 5}
|
||||
// CHECK:STDOUT: !3 = !{i32 2, !"Debug Info Version", i32 3}
|
||||
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.C.Main", scope: null, file: !1, line: 10, type: !5, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
|
||||
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
|
||||
// CHECK:STDOUT: !6 = !{!7, !8}
|
||||
// CHECK:STDOUT: !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
|
||||
// CHECK:STDOUT: !8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
|
||||
// CHECK:STDOUT: !9 = !{!10}
|
||||
// CHECK:STDOUT: !10 = !DILocalVariable(arg: 1, scope: !4, type: !8)
|
||||
// CHECK:STDOUT: !11 = !DILocation(line: 10, column: 30, scope: !4)
|
||||
// CHECK:STDOUT: !12 = !DILocation(line: 10, column: 23, scope: !4)
|
||||
// CHECK:STDOUT: !13 = distinct !DISubprogram(name: "Call2", linkageName: "_CCall2.Main", scope: null, file: !1, line: 21, type: !14, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !16)
|
||||
// CHECK:STDOUT: !14 = !DISubroutineType(types: !15)
|
||||
// CHECK:STDOUT: !15 = !{null, !8}
|
||||
// CHECK:STDOUT: !16 = !{!17}
|
||||
// CHECK:STDOUT: !17 = !DILocalVariable(arg: 1, scope: !13, type: !8)
|
||||
// CHECK:STDOUT: !18 = !DILocation(line: 22, column: 3, scope: !13)
|
||||
// CHECK:STDOUT: !19 = !DILocation(line: 21, column: 1, scope: !13)
|
||||
// CHECK:STDOUT: !20 = distinct !DISubprogram(name: "Call", linkageName: "_CCall.Main.0676fa98bb7dbd66", scope: null, file: !1, line: 17, type: !5, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !21)
|
||||
// CHECK:STDOUT: !21 = !{!22}
|
||||
// CHECK:STDOUT: !22 = !DILocalVariable(arg: 1, scope: !20, type: !8)
|
||||
// CHECK:STDOUT: !23 = !DILocation(line: 18, column: 10, scope: !20)
|
||||
// CHECK:STDOUT: !24 = !DILocation(line: 18, column: 3, scope: !20)
|
||||
// CHECK:STDOUT:
|
||||
|
||||
Reference in New Issue
Block a user