Procházet zdrojové kódy

Handle impl with bad interface on import (#5051)

Co-authored-by: Josh L <josh11b@users.noreply.github.com>
josh11b před 1 rokem
rodič
revize
331f55f0a2

+ 11 - 4
toolchain/check/import_ref.cpp

@@ -2134,10 +2134,14 @@ struct SpecificInterfaceData {
 static auto GetLocalSpecificInstanceData(
     ImportRefResolver& resolver, SemIR::SpecificInterface import_interface)
     -> SpecificInterfaceData {
-  return {.interface_const_id = GetLocalConstantId(
-              resolver, resolver.import_interfaces()
-                            .Get(import_interface.interface_id)
-                            .first_owning_decl_id),
+  SemIR::ConstantId interface_const_id = SemIR::ConstantId::None;
+  if (import_interface.interface_id.has_value()) {
+    interface_const_id =
+        GetLocalConstantId(resolver, resolver.import_interfaces()
+                                         .Get(import_interface.interface_id)
+                                         .first_owning_decl_id);
+  }
+  return {.interface_const_id = interface_const_id,
           .specific_data =
               GetLocalSpecificData(resolver, import_interface.specific_id)};
 }
@@ -2146,6 +2150,9 @@ static auto GetLocalSpecificInterface(ImportContext& context,
                                       SemIR::SpecificId import_specific_id,
                                       SpecificInterfaceData interface_data)
     -> SemIR::SpecificInterface {
+  if (!interface_data.interface_const_id.has_value()) {
+    return SemIR::SpecificInterface::None;
+  }
   // Find the corresponding interface type. For a non-generic interface,
   // this is the type of the interface declaration. For a generic interface,
   // build a interface type referencing this specialization of the generic

+ 39 - 0
toolchain/check/testdata/impl/no_prelude/import_impl_with_no_interface.carbon

@@ -0,0 +1,39 @@
+// 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
+//
+// EXTRA-ARGS: --no-dump-sem-ir
+//
+// AUTOUPDATE
+// TIP: To test this file alone, run:
+// TIP:   bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/no_prelude/import_impl_with_no_interface.carbon
+// TIP: To dump output, run:
+// TIP:   bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/no_prelude/import_impl_with_no_interface.carbon
+
+// --- fail_lib.carbon
+library "[[@TEST_NAME]]";
+
+// CHECK:STDERR: fail_lib.carbon:[[@LINE+4]]:18: error: name `Undeclared` not found [NameNotFound]
+// CHECK:STDERR: impl {.i: ()} as Undeclared {
+// CHECK:STDERR:                  ^~~~~~~~~~
+// CHECK:STDERR:
+impl {.i: ()} as Undeclared {
+  fn F() {}
+}
+
+interface Instance {
+  fn G[self: Self]();
+}
+
+impl {.i: ()} as Instance {
+  fn G[self: Self]() {}
+}
+
+// --- import_instance_success.carbon
+library "[[@TEST_NAME]]";
+
+import library "lib";
+
+fn InstanceCallImport(n: {.i: ()}) {
+  n.(Instance.G)();
+}