impl_lookup.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/impl_lookup.h"
  5. #include <algorithm>
  6. #include <functional>
  7. #include <utility>
  8. #include <variant>
  9. #include "toolchain/base/kind_switch.h"
  10. #include "toolchain/check/cpp/impl_lookup.h"
  11. #include "toolchain/check/deduce.h"
  12. #include "toolchain/check/diagnostic_helpers.h"
  13. #include "toolchain/check/eval.h"
  14. #include "toolchain/check/facet_type.h"
  15. #include "toolchain/check/generic.h"
  16. #include "toolchain/check/impl.h"
  17. #include "toolchain/check/import_ref.h"
  18. #include "toolchain/check/inst.h"
  19. #include "toolchain/check/subst.h"
  20. #include "toolchain/check/type.h"
  21. #include "toolchain/check/type_completion.h"
  22. #include "toolchain/check/type_structure.h"
  23. #include "toolchain/sem_ir/facet_type_info.h"
  24. #include "toolchain/sem_ir/ids.h"
  25. #include "toolchain/sem_ir/impl.h"
  26. #include "toolchain/sem_ir/inst.h"
  27. #include "toolchain/sem_ir/typed_insts.h"
  28. namespace Carbon::Check {
  29. // Returns IRs which are allowed to define an `impl` involving the arguments.
  30. // This is limited by the orphan rule.
  31. static auto FindAssociatedImportIRs(
  32. Context& context, SemIR::ConstantId query_self_const_id,
  33. SemIR::SpecificInterface query_specific_interface)
  34. -> llvm::SmallVector<SemIR::ImportIRId> {
  35. llvm::SmallVector<SemIR::ImportIRId> result;
  36. // Add an entity to our result.
  37. auto add_entity = [&](const SemIR::EntityWithParamsBase& entity) {
  38. // We will look for impls in the import IR associated with the first owning
  39. // declaration.
  40. auto decl_id = entity.first_owning_decl_id;
  41. if (!decl_id.has_value()) {
  42. return;
  43. }
  44. auto import_ir_inst = GetCanonicalImportIRInst(context, decl_id);
  45. const auto* sem_ir = &context.sem_ir();
  46. if (import_ir_inst.ir_id().has_value()) {
  47. sem_ir = context.import_irs().Get(import_ir_inst.ir_id()).sem_ir;
  48. }
  49. // For an instruction imported from C++, `GetCanonicalImportIRInst` returns
  50. // the final Carbon import instruction, so go one extra step to check for a
  51. // C++ import.
  52. if (auto import_ir_inst_id =
  53. sem_ir->insts().GetImportSource(import_ir_inst.inst_id());
  54. import_ir_inst_id.has_value()) {
  55. result.push_back(
  56. sem_ir->import_ir_insts().Get(import_ir_inst_id).ir_id());
  57. } else if (import_ir_inst.ir_id().has_value()) {
  58. result.push_back(import_ir_inst.ir_id());
  59. }
  60. };
  61. llvm::SmallVector<SemIR::InstId> worklist;
  62. // Push the contents of an instruction block onto our worklist.
  63. auto push_block = [&](SemIR::InstBlockId block_id) {
  64. if (block_id.has_value()) {
  65. llvm::append_range(worklist, context.inst_blocks().Get(block_id));
  66. }
  67. };
  68. // Add the arguments of a specific to the worklist.
  69. auto push_args = [&](SemIR::SpecificId specific_id) {
  70. if (specific_id.has_value()) {
  71. push_block(context.specifics().Get(specific_id).args_id);
  72. }
  73. };
  74. worklist.push_back(context.constant_values().GetInstId(query_self_const_id));
  75. add_entity(context.interfaces().Get(query_specific_interface.interface_id));
  76. push_args(query_specific_interface.specific_id);
  77. while (!worklist.empty()) {
  78. auto inst_id = worklist.pop_back_val();
  79. // Visit the operands of the constant.
  80. auto inst = context.insts().Get(inst_id);
  81. for (auto arg : {inst.arg0_and_kind(), inst.arg1_and_kind()}) {
  82. CARBON_KIND_SWITCH(arg) {
  83. case CARBON_KIND(SemIR::InstId inst_id): {
  84. if (inst_id.has_value()) {
  85. worklist.push_back(inst_id);
  86. }
  87. break;
  88. }
  89. case CARBON_KIND(SemIR::TypeInstId inst_id): {
  90. if (inst_id.has_value()) {
  91. worklist.push_back(inst_id);
  92. }
  93. break;
  94. }
  95. case CARBON_KIND(SemIR::InstBlockId inst_block_id): {
  96. push_block(inst_block_id);
  97. break;
  98. }
  99. case CARBON_KIND(SemIR::ClassId class_id): {
  100. add_entity(context.classes().Get(class_id));
  101. break;
  102. }
  103. case CARBON_KIND(SemIR::InterfaceId interface_id): {
  104. add_entity(context.interfaces().Get(interface_id));
  105. break;
  106. }
  107. case CARBON_KIND(SemIR::FacetTypeId facet_type_id): {
  108. const auto& facet_type_info =
  109. context.facet_types().Get(facet_type_id);
  110. for (const auto& impl : facet_type_info.extend_constraints) {
  111. add_entity(context.interfaces().Get(impl.interface_id));
  112. push_args(impl.specific_id);
  113. }
  114. for (const auto& impl : facet_type_info.self_impls_constraints) {
  115. add_entity(context.interfaces().Get(impl.interface_id));
  116. push_args(impl.specific_id);
  117. }
  118. break;
  119. }
  120. case CARBON_KIND(SemIR::FunctionId function_id): {
  121. add_entity(context.functions().Get(function_id));
  122. break;
  123. }
  124. case CARBON_KIND(SemIR::SpecificId specific_id): {
  125. push_args(specific_id);
  126. break;
  127. }
  128. default: {
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. // Deduplicate.
  135. llvm::sort(result, [](SemIR::ImportIRId a, SemIR::ImportIRId b) {
  136. return a.index < b.index;
  137. });
  138. result.erase(llvm::unique(result), result.end());
  139. return result;
  140. }
  141. // Returns true if a cycle was found and diagnosed.
  142. static auto FindAndDiagnoseImplLookupCycle(
  143. Context& context,
  144. const llvm::SmallVector<Context::ImplLookupStackEntry>& stack,
  145. SemIR::LocId loc_id, SemIR::ConstantId query_self_const_id,
  146. SemIR::ConstantId query_facet_type_const_id) -> bool {
  147. // Deduction of the interface parameters can do further impl lookups, and we
  148. // need to ensure we terminate.
  149. //
  150. // https://docs.carbon-lang.dev/docs/design/generics/details.html#acyclic-rule
  151. // - We look for violations of the acyclic rule by seeing if a previous lookup
  152. // had all the same type inputs.
  153. // - The `query_facet_type_const_id` encodes the entire facet type being
  154. // looked up, including any specific parameters for a generic interface.
  155. //
  156. // TODO: Implement the termination rule, which requires looking at the
  157. // complexity of the types on the top of (or throughout?) the stack:
  158. // https://docs.carbon-lang.dev/docs/design/generics/details.html#termination-rule
  159. for (auto [i, entry] : llvm::enumerate(stack)) {
  160. if (entry.query_self_const_id == query_self_const_id &&
  161. entry.query_facet_type_const_id == query_facet_type_const_id) {
  162. auto facet_type_type_id =
  163. context.types().GetTypeIdForTypeConstantId(query_facet_type_const_id);
  164. CARBON_DIAGNOSTIC(ImplLookupCycle, Error,
  165. "cycle found in search for impl of {0} for type {1}",
  166. SemIR::TypeId, SemIR::TypeId);
  167. auto builder = context.emitter().Build(
  168. loc_id, ImplLookupCycle, facet_type_type_id,
  169. context.types().GetTypeIdForTypeConstantId(query_self_const_id));
  170. for (const auto& active_entry : llvm::drop_begin(stack, i)) {
  171. if (active_entry.impl_loc.has_value()) {
  172. CARBON_DIAGNOSTIC(ImplLookupCycleNote, Note,
  173. "determining if this impl clause matches", );
  174. builder.Note(active_entry.impl_loc, ImplLookupCycleNote);
  175. }
  176. }
  177. builder.Emit();
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. struct InterfacesFromConstantId {
  184. llvm::ArrayRef<SemIR::SpecificInterface> interfaces;
  185. SemIR::BuiltinConstraintMask builtin_constraint_mask;
  186. bool other_requirements;
  187. };
  188. // Gets the set of `SpecificInterface`s that are required by a facet type
  189. // (as a constant value), and any special requirements.
  190. static auto GetInterfacesFromConstantId(
  191. Context& context, SemIR::LocId loc_id,
  192. SemIR::ConstantId query_facet_type_const_id)
  193. -> std::optional<InterfacesFromConstantId> {
  194. auto facet_type_inst_id =
  195. context.constant_values().GetInstId(query_facet_type_const_id);
  196. auto facet_type_inst =
  197. context.insts().GetAs<SemIR::FacetType>(facet_type_inst_id);
  198. const auto& facet_type_info =
  199. context.facet_types().Get(facet_type_inst.facet_type_id);
  200. auto identified_id =
  201. RequireIdentifiedFacetType(context, loc_id, facet_type_inst, [&] {
  202. CARBON_DIAGNOSTIC(ImplLookupInUnidentifiedFacetType, Error,
  203. "facet type {0} can not be identified", InstIdAsType);
  204. return context.emitter().Build(
  205. loc_id, ImplLookupInUnidentifiedFacetType, facet_type_inst_id);
  206. });
  207. if (!identified_id.has_value()) {
  208. return std::nullopt;
  209. }
  210. return {{.interfaces = context.identified_facet_types()
  211. .Get(identified_id)
  212. .required_interfaces(),
  213. .builtin_constraint_mask = facet_type_info.builtin_constraint_mask,
  214. .other_requirements = facet_type_info.other_requirements}};
  215. }
  216. static auto GetWitnessIdForImpl(Context& context, SemIR::LocId loc_id,
  217. bool query_is_concrete,
  218. SemIR::ConstantId query_self_const_id,
  219. const SemIR::SpecificInterface& interface,
  220. SemIR::ImplId impl_id) -> EvalImplLookupResult {
  221. const SemIR::Impl& impl = context.impls().Get(impl_id);
  222. // The impl may have generic arguments, in which case we need to deduce them
  223. // to find what they are given the specific type and interface query. We use
  224. // that specific to map values in the impl to the deduced values.
  225. auto specific_id = SemIR::SpecificId::None;
  226. if (impl.generic_id.has_value()) {
  227. specific_id = DeduceImplArguments(
  228. context, loc_id, impl, query_self_const_id, interface.specific_id);
  229. if (!specific_id.has_value()) {
  230. return EvalImplLookupResult::MakeNone();
  231. }
  232. }
  233. // The self type of the impl must match the type in the query, or this is an
  234. // `impl T as ...` for some other type `T` and should not be considered.
  235. auto noncanonical_deduced_self_const_id = SemIR::GetConstantValueInSpecific(
  236. context.sem_ir(), specific_id, impl.self_id);
  237. // In a generic `impl forall` the self type can be a FacetAccessType, which
  238. // will not be the same constant value as a query facet value. We move through
  239. // to the facet value here, and if the query was a FacetAccessType we did the
  240. // same there so they still match.
  241. auto deduced_self_const_id =
  242. GetCanonicalFacetOrTypeValue(context, noncanonical_deduced_self_const_id);
  243. if (query_self_const_id != deduced_self_const_id) {
  244. return EvalImplLookupResult::MakeNone();
  245. }
  246. // The impl's constraint is a facet type which it is implementing for the self
  247. // type: the `I` in `impl ... as I`. The deduction step may be unable to be
  248. // fully applied to the types in the constraint and result in an error here,
  249. // in which case it does not match the query.
  250. auto deduced_constraint_id =
  251. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  252. context.sem_ir(), specific_id, impl.constraint_id));
  253. if (deduced_constraint_id == SemIR::ErrorInst::InstId) {
  254. return EvalImplLookupResult::MakeNone();
  255. }
  256. auto deduced_constraint_facet_type_id =
  257. context.insts()
  258. .GetAs<SemIR::FacetType>(deduced_constraint_id)
  259. .facet_type_id;
  260. const auto& deduced_constraint_facet_type_info =
  261. context.facet_types().Get(deduced_constraint_facet_type_id);
  262. CARBON_CHECK(deduced_constraint_facet_type_info.extend_constraints.size() ==
  263. 1);
  264. if (deduced_constraint_facet_type_info.other_requirements ||
  265. !deduced_constraint_facet_type_info.builtin_constraint_mask.empty()) {
  266. return EvalImplLookupResult::MakeNone();
  267. }
  268. // The specifics in the queried interface must match the deduced specifics in
  269. // the impl's constraint facet type.
  270. auto impl_interface_specific_id =
  271. deduced_constraint_facet_type_info.extend_constraints[0].specific_id;
  272. auto query_interface_specific_id = interface.specific_id;
  273. if (impl_interface_specific_id != query_interface_specific_id) {
  274. return EvalImplLookupResult::MakeNone();
  275. }
  276. LoadImportRef(context, impl.witness_id);
  277. if (specific_id.has_value()) {
  278. // Add an instruction to support requiring an impl definition which may not
  279. // otherwise be generated. This is used to resolve dependency chains when
  280. // `MakeFinal` is returned without a concrete definition; particularly final
  281. // impls with symbolic constants.
  282. AddInstInNoBlock(
  283. context, loc_id,
  284. SemIR::RequireSpecificDefinition{
  285. .type_id = GetSingletonType(
  286. context, SemIR::RequireSpecificDefinitionType::TypeInstId),
  287. .specific_id = specific_id});
  288. // We need a definition of the specific `impl` so we can access its
  289. // witness.
  290. ResolveSpecificDefinition(context, loc_id, specific_id);
  291. }
  292. if (query_is_concrete || impl.is_final) {
  293. // TODO: These final results should be cached somehow. Positive (non-None)
  294. // results could be cached globally, as they can not change. But
  295. // negative results can change after a final impl is written, so
  296. // they can only be cached in a limited way, or the cache needs to
  297. // be invalidated by writing a final impl that would match.
  298. return EvalImplLookupResult::MakeFinal(
  299. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  300. context.sem_ir(), specific_id, impl.witness_id)));
  301. } else {
  302. return EvalImplLookupResult::MakeNonFinal();
  303. }
  304. }
  305. // Finds a lookup result from `query_self_inst_id` if it is a facet value that
  306. // names the query interface in its facet type. Note that `query_self_inst_id`
  307. // is allowed to be a non-canonical facet value in order to find a concrete
  308. // witness, so it's not referenced as a constant value.
  309. static auto LookupImplWitnessInSelfFacetValue(
  310. Context& context, SemIR::LocId loc_id,
  311. SemIR::InstId self_facet_value_inst_id,
  312. SemIR::SpecificInterface query_specific_interface) -> EvalImplLookupResult {
  313. auto facet_type = context.types().TryGetAs<SemIR::FacetType>(
  314. context.insts().Get(self_facet_value_inst_id).type_id());
  315. if (!facet_type) {
  316. return EvalImplLookupResult::MakeNone();
  317. }
  318. // The position of the interface in `required_interfaces()` is also the
  319. // position of the witness for that interface in `FacetValue`. The
  320. // `FacetValue` witnesses are the output of an impl lookup, which finds and
  321. // returns witnesses in the same order.
  322. auto identified_id =
  323. RequireIdentifiedFacetType(context, loc_id, *facet_type, nullptr);
  324. // This should not be possible as FacetValue is constructed by a conversion
  325. // to a facet type, which performs impl lookup for that facet type, and
  326. // lookup only succeeds for complete facet types.
  327. CARBON_CHECK(identified_id.has_value(),
  328. "FacetValue was constructed with an incomplete facet type");
  329. auto facet_type_required_interfaces =
  330. llvm::enumerate(context.identified_facet_types()
  331. .Get(identified_id)
  332. .required_interfaces());
  333. auto it = llvm::find_if(facet_type_required_interfaces, [=](auto e) {
  334. return e.value() == query_specific_interface;
  335. });
  336. if (it == facet_type_required_interfaces.end()) {
  337. return EvalImplLookupResult::MakeNone();
  338. }
  339. auto index = (*it).index();
  340. if (auto facet_value = context.insts().TryGetAs<SemIR::FacetValue>(
  341. self_facet_value_inst_id)) {
  342. auto witness_id =
  343. context.inst_blocks().Get(facet_value->witnesses_block_id)[index];
  344. if (context.insts().Is<SemIR::ImplWitness>(witness_id)) {
  345. return EvalImplLookupResult::MakeFinal(witness_id);
  346. }
  347. }
  348. return EvalImplLookupResult::MakeNonFinal();
  349. }
  350. // Substitutes witnesess in place of `LookupImplWitness` queries into `.Self`,
  351. // when the witness is for the same interface as the one `.Self` is referring
  352. // to.
  353. //
  354. // This allows access to the `FacetType` and its constraints from the witness,
  355. // and allows `ImplWitnessAccess` instructions to be immediately resolved to a
  356. // more specific value when possible.
  357. class SubstWitnessesCallbacks : public SubstInstCallbacks {
  358. public:
  359. // `context` must not be null.
  360. explicit SubstWitnessesCallbacks(
  361. Context* context, SemIR::LocId loc_id,
  362. llvm::ArrayRef<SemIR::SpecificInterface> interfaces,
  363. llvm::ArrayRef<SemIR::InstId> witness_inst_ids)
  364. : SubstInstCallbacks(context),
  365. loc_id_(loc_id),
  366. interfaces_(interfaces),
  367. witness_inst_ids_(witness_inst_ids) {}
  368. auto Subst(SemIR::InstId& inst_id) -> SubstResult override {
  369. // `FacetType` can be concrete even when it has rewrite constraints that
  370. // have a symbolic dependency on `.Self`. See use of
  371. // `GetConstantValueIgnoringPeriodSelf` in eval. So in order to recurse into
  372. // `FacetType` we must check for it before the `is_concrete` early return.
  373. if (context().insts().Is<SemIR::FacetType>(inst_id)) {
  374. ++facet_type_depth_;
  375. return SubstOperands;
  376. }
  377. if (context().constant_values().Get(inst_id).is_concrete()) {
  378. return FullySubstituted;
  379. }
  380. auto access = context().insts().TryGetAs<SemIR::ImplWitnessAccess>(inst_id);
  381. if (!access) {
  382. return SubstOperands;
  383. }
  384. auto lookup =
  385. context().insts().GetAs<SemIR::LookupImplWitness>(access->witness_id);
  386. auto bind_name = context().insts().TryGetAs<SemIR::SymbolicBinding>(
  387. lookup.query_self_inst_id);
  388. if (!bind_name) {
  389. return SubstOperands;
  390. }
  391. const auto& self_entity_name =
  392. context().entity_names().Get(bind_name->entity_name_id);
  393. if (self_entity_name.name_id != SemIR::NameId::PeriodSelf) {
  394. return SubstOperands;
  395. }
  396. // TODO: Once we are numbering `EntityName`, (see the third model in
  397. // https://docs.google.com/document/d/1Yt-i5AmF76LSvD4TrWRIAE_92kii6j5yFiW-S7ahzlg/edit?tab=t.0#heading=h.7urbxcq23olv)
  398. // then verify that the index here is equal to the `facet_type_depth_`,
  399. // which would mean that it is a reference to the top-level `Self`, which is
  400. // being replaced with the impl lookup query self facet value (and then we
  401. // use the witness derived from it).
  402. //
  403. // For now, we only substitute if depth == 0, which is incorrect inside
  404. // nested facet types, as it can miss references in specifics up to the top
  405. // level facet value.
  406. if (facet_type_depth_ > 0) {
  407. return SubstOperands;
  408. }
  409. auto witness_id =
  410. FindWitnessForInterface(lookup.query_specific_interface_id);
  411. if (!witness_id.has_value()) {
  412. return SubstOperands;
  413. }
  414. inst_id = RebuildNewInst(
  415. context().insts().GetLocIdForDesugaring(loc_id_),
  416. SemIR::ImplWitnessAccess{.type_id = GetSingletonType(
  417. context(), SemIR::WitnessType::TypeInstId),
  418. .witness_id = witness_id,
  419. .index = access->index});
  420. // Once we replace a witness, we either have a concrete value or some
  421. // reference to an associated constant that came from the witness's facet
  422. // type. We don't want to substitute into the witness's facet type, so we
  423. // don't recurse on whatever came from the witness.
  424. return FullySubstituted;
  425. }
  426. auto Rebuild(SemIR::InstId orig_inst_id, SemIR::Inst new_inst)
  427. -> SemIR::InstId override {
  428. if (context().insts().Is<SemIR::FacetType>(orig_inst_id)) {
  429. --facet_type_depth_;
  430. }
  431. return RebuildNewInst(loc_id_, new_inst);
  432. }
  433. auto ReuseUnchanged(SemIR::InstId orig_inst_id) -> SemIR::InstId override {
  434. if (context().insts().Is<SemIR::FacetType>(orig_inst_id)) {
  435. --facet_type_depth_;
  436. }
  437. return orig_inst_id;
  438. }
  439. private:
  440. auto FindWitnessForInterface(SemIR::SpecificInterfaceId specific_interface_id)
  441. -> SemIR::InstId {
  442. auto lookup_query_interface =
  443. context().specific_interfaces().Get(specific_interface_id);
  444. for (auto [interface, witness_inst_id] :
  445. llvm::zip_equal(interfaces_, witness_inst_ids_)) {
  446. // If the `LookupImplWitness` for `.Self` is not looking for the same
  447. // interface as we have a witness for, this is not the right witness to
  448. // use to replace the lookup for `.Self`.
  449. if (interface.interface_id == lookup_query_interface.interface_id) {
  450. return witness_inst_id;
  451. }
  452. }
  453. return SemIR::InstId::None;
  454. }
  455. SemIR::LocId loc_id_;
  456. llvm::ArrayRef<SemIR::SpecificInterface> interfaces_;
  457. llvm::ArrayRef<SemIR::InstId> witness_inst_ids_;
  458. int facet_type_depth_ = 0;
  459. };
  460. static auto VerifyQueryFacetTypeConstraints(
  461. Context& context, SemIR::LocId loc_id,
  462. SemIR::InstId query_facet_type_inst_id,
  463. llvm::ArrayRef<SemIR::SpecificInterface> interfaces,
  464. llvm::ArrayRef<SemIR::InstId> witness_inst_ids) -> bool {
  465. CARBON_CHECK(context.insts().Is<SemIR::FacetType>(query_facet_type_inst_id));
  466. const auto& facet_type_info = context.facet_types().Get(
  467. context.insts()
  468. .GetAs<SemIR::FacetType>(query_facet_type_inst_id)
  469. .facet_type_id);
  470. if (!facet_type_info.rewrite_constraints.empty()) {
  471. auto callbacks =
  472. SubstWitnessesCallbacks(&context, loc_id, interfaces, witness_inst_ids);
  473. for (const auto& rewrite : facet_type_info.rewrite_constraints) {
  474. auto lhs_id = SubstInst(context, rewrite.lhs_id, callbacks);
  475. auto rhs_id = SubstInst(context, rewrite.rhs_id, callbacks);
  476. if (lhs_id != rhs_id) {
  477. // TODO: Provide a diagnostic note and location for which rewrite
  478. // constraint was not satisfied, if a diagnostic is going to be
  479. // displayed for the LookupImplWitessFailure. This will require plumbing
  480. // through a callback that lets us add a Note to another diagnostic.
  481. return false;
  482. }
  483. }
  484. }
  485. // TODO: Validate that the witnesses satisfy the other requirements in the
  486. // `facet_type_info`.
  487. return true;
  488. }
  489. // Begin a search for an impl declaration matching the query. We do this by
  490. // creating an LookupImplWitness instruction and evaluating. If it's able to
  491. // find a final concrete impl, then it will evaluate to that `ImplWitness` but
  492. // if not, it will evaluate to itself as a symbolic witness to be further
  493. // evaluated with a more specific query when building a specific for the generic
  494. // context the query came from.
  495. static auto GetOrAddLookupImplWitness(Context& context, SemIR::LocId loc_id,
  496. SemIR::ConstantId query_self_const_id,
  497. SemIR::SpecificInterface interface)
  498. -> SemIR::InstId {
  499. auto witness_const_id = EvalOrAddInst(
  500. context, context.insts().GetLocIdForDesugaring(loc_id),
  501. SemIR::LookupImplWitness{
  502. .type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
  503. .query_self_inst_id =
  504. context.constant_values().GetInstId(query_self_const_id),
  505. .query_specific_interface_id =
  506. context.specific_interfaces().Add(interface),
  507. });
  508. // We use a NotConstant result from eval to communicate back an impl
  509. // lookup failure. See `EvalConstantInst()` for `LookupImplWitness`.
  510. if (!witness_const_id.is_constant()) {
  511. return SemIR::InstId::None;
  512. }
  513. return context.constant_values().GetInstId(witness_const_id);
  514. }
  515. // Returns true if the `Self` should impl `Destroy`.
  516. static auto TypeCanDestroy(Context& context,
  517. SemIR::ConstantId query_self_const_id) -> bool {
  518. auto inst = context.insts().Get(context.constant_values().GetInstId(
  519. GetCanonicalFacetOrTypeValue(context, query_self_const_id)));
  520. // For facet values, look if the FacetType provides the same.
  521. if (auto facet_type =
  522. context.types().TryGetAs<SemIR::FacetType>(inst.type_id())) {
  523. const auto& info = context.facet_types().Get(facet_type->facet_type_id);
  524. if (info.builtin_constraint_mask.HasAnyOf(
  525. SemIR::BuiltinConstraintMask::TypeCanDestroy)) {
  526. return true;
  527. }
  528. }
  529. CARBON_KIND_SWITCH(inst) {
  530. case CARBON_KIND(SemIR::ClassType class_type): {
  531. auto class_info = context.classes().Get(class_type.class_id);
  532. // Incomplete and abstract classes can't be destroyed.
  533. // TODO: Return false if the object repr doesn't impl `Destroy`.
  534. // TODO: Return false for C++ types that lack a destructor.
  535. return class_info.is_complete() &&
  536. class_info.inheritance_kind !=
  537. SemIR::Class::InheritanceKind::Abstract;
  538. }
  539. case SemIR::ArrayType::Kind:
  540. case SemIR::ConstType::Kind:
  541. case SemIR::MaybeUnformedType::Kind:
  542. case SemIR::PartialType::Kind:
  543. case SemIR::StructType::Kind:
  544. case SemIR::TupleType::Kind:
  545. // TODO: Return false for types that indirectly reference a type that
  546. // doesn't impl `Destroy`.
  547. return true;
  548. case SemIR::BoolType::Kind:
  549. case SemIR::PointerType::Kind:
  550. // Trivially destructible.
  551. return true;
  552. default:
  553. return false;
  554. }
  555. }
  556. auto LookupImplWitness(Context& context, SemIR::LocId loc_id,
  557. SemIR::ConstantId query_self_const_id,
  558. SemIR::ConstantId query_facet_type_const_id)
  559. -> SemIR::InstBlockIdOrError {
  560. if (query_self_const_id == SemIR::ErrorInst::ConstantId ||
  561. query_facet_type_const_id == SemIR::ErrorInst::ConstantId) {
  562. return SemIR::InstBlockIdOrError::MakeError();
  563. }
  564. {
  565. // The query self value is a type value or a facet value.
  566. auto query_self_type_id =
  567. context.insts()
  568. .Get(context.constant_values().GetInstId(query_self_const_id))
  569. .type_id();
  570. CARBON_CHECK(context.types().Is<SemIR::TypeType>(query_self_type_id) ||
  571. context.types().Is<SemIR::FacetType>(query_self_type_id));
  572. // The query facet type value is indeed a facet type.
  573. CARBON_CHECK(context.insts().Is<SemIR::FacetType>(
  574. context.constant_values().GetInstId(query_facet_type_const_id)));
  575. }
  576. auto interfaces_from_constant_id =
  577. GetInterfacesFromConstantId(context, loc_id, query_facet_type_const_id);
  578. if (!interfaces_from_constant_id) {
  579. return SemIR::InstBlockIdOrError::MakeError();
  580. }
  581. auto [interfaces, builtin_constraint_mask, other_requirements] =
  582. *interfaces_from_constant_id;
  583. if (other_requirements) {
  584. // TODO: Remove this when other requirements go away.
  585. return SemIR::InstBlockId::None;
  586. }
  587. if (builtin_constraint_mask.HasAnyOf(
  588. SemIR::BuiltinConstraintMask::TypeCanDestroy) &&
  589. !TypeCanDestroy(context, query_self_const_id)) {
  590. return SemIR::InstBlockId::None;
  591. }
  592. if (interfaces.empty()) {
  593. return SemIR::InstBlockId::Empty;
  594. }
  595. if (FindAndDiagnoseImplLookupCycle(context, context.impl_lookup_stack(),
  596. loc_id, query_self_const_id,
  597. query_facet_type_const_id)) {
  598. return SemIR::InstBlockIdOrError::MakeError();
  599. }
  600. auto& stack = context.impl_lookup_stack();
  601. stack.push_back({
  602. .query_self_const_id = query_self_const_id,
  603. .query_facet_type_const_id = query_facet_type_const_id,
  604. });
  605. // We need to find a witness for each interface in `interfaces`. Every
  606. // consumer of a facet type needs to agree on the order of interfaces used for
  607. // its witnesses.
  608. llvm::SmallVector<SemIR::InstId> result_witness_ids;
  609. for (const auto& interface : interfaces) {
  610. // TODO: Since both `interfaces` and `query_self_const_id` are sorted lists,
  611. // do an O(N+M) merge instead of O(N*M) nested loops.
  612. auto result_witness_id = GetOrAddLookupImplWitness(
  613. context, loc_id, query_self_const_id, interface);
  614. if (result_witness_id.has_value()) {
  615. result_witness_ids.push_back(result_witness_id);
  616. } else {
  617. // At least one queried interface in the facet type has no witness for the
  618. // given type, we can stop looking for more.
  619. break;
  620. }
  621. }
  622. stack.pop_back();
  623. // All interfaces in the query facet type must have been found to be available
  624. // through some impl, or directly on the value's facet type if
  625. // `query_self_const_id` is a facet value.
  626. if (result_witness_ids.size() != interfaces.size()) {
  627. return SemIR::InstBlockId::None;
  628. }
  629. // Verify rewrite constraints in the query constraint are satisfied after
  630. // applying the rewrites from the found witnesses.
  631. if (!VerifyQueryFacetTypeConstraints(
  632. context, loc_id,
  633. context.constant_values().GetInstId(query_facet_type_const_id),
  634. interfaces, result_witness_ids)) {
  635. return SemIR::InstBlockId::None;
  636. }
  637. return context.inst_blocks().AddCanonical(result_witness_ids);
  638. }
  639. // Returns whether the query is concrete, it is false if the self type or
  640. // interface specifics have a symbolic dependency.
  641. static auto QueryIsConcrete(Context& context, SemIR::ConstantId self_const_id,
  642. const SemIR::SpecificInterface& specific_interface)
  643. -> bool {
  644. if (!self_const_id.is_concrete()) {
  645. return false;
  646. }
  647. if (!specific_interface.specific_id.has_value()) {
  648. return true;
  649. }
  650. auto args_id =
  651. context.specifics().Get(specific_interface.specific_id).args_id;
  652. for (auto inst_id : context.inst_blocks().Get(args_id)) {
  653. if (!context.constant_values().Get(inst_id).is_concrete()) {
  654. return false;
  655. }
  656. }
  657. return true;
  658. }
  659. namespace {
  660. // A class to filter imported impls based on whether they could possibly match a
  661. // query, prior to importing them. For now we only consider impls that are for
  662. // an interface that's being queried.
  663. //
  664. // TODO: There's a lot more we could do to filter out impls that can't possibly
  665. // match.
  666. class ImportImplFilter {
  667. public:
  668. explicit ImportImplFilter(Context& context, SemIR::ImportIRId import_ir_id,
  669. SemIR::SpecificInterface interface)
  670. : context_(&context),
  671. interface_id_(interface.interface_id),
  672. import_ir_id_(import_ir_id),
  673. import_ir_(context_->import_irs().Get(import_ir_id).sem_ir),
  674. cached_import_interface_id_(SemIR::InterfaceId::None) {}
  675. // Returns whether the given impl is potentially relevant for the current
  676. // query.
  677. auto IsRelevantImpl(SemIR::ImplId import_impl_id) -> bool {
  678. auto impl_interface_id =
  679. import_ir_->impls().Get(import_impl_id).interface.interface_id;
  680. if (!impl_interface_id.has_value()) {
  681. // This indicates that an error occurred when type-checking the impl.
  682. // TODO: Use an explicit error value for this rather than None.
  683. return false;
  684. }
  685. return IsRelevantInterface(impl_interface_id);
  686. }
  687. private:
  688. // Returns whether an impl for the given interface might be relevant to the
  689. // current query.
  690. auto IsRelevantInterface(SemIR::InterfaceId import_interface_id) -> bool {
  691. if (!cached_import_interface_id_.has_value()) {
  692. if (IsSameInterface(import_interface_id, interface_id_)) {
  693. cached_import_interface_id_ = import_interface_id;
  694. return true;
  695. }
  696. } else if (cached_import_interface_id_ == import_interface_id) {
  697. return true;
  698. }
  699. return false;
  700. }
  701. // Returns whether the given interfaces from two different IRs are the same.
  702. auto IsSameInterface(SemIR::InterfaceId import_interface_id,
  703. SemIR::InterfaceId local_interface_id) -> bool {
  704. // The names must be the same.
  705. if (import_ir_->names().GetAsStringIfIdentifier(
  706. import_ir_->interfaces().Get(import_interface_id).name_id) !=
  707. context_->names().GetAsStringIfIdentifier(
  708. context_->interfaces().Get(local_interface_id).name_id)) {
  709. return false;
  710. }
  711. // Compare the interfaces themselves.
  712. // TODO: Should we check the scope of the interface before doing this?
  713. auto local_version_of_import_interface_id =
  714. ImportInterface(*context_, import_ir_id_, import_interface_id);
  715. return local_version_of_import_interface_id == local_interface_id;
  716. }
  717. Context* context_;
  718. // The interface being looked up.
  719. SemIR::InterfaceId interface_id_;
  720. // The IR that we are currently importing impls from.
  721. SemIR::ImportIRId import_ir_id_;
  722. const SemIR::File* import_ir_;
  723. // The interface ID of `interface_id_` in `import_ir_`, if known.
  724. SemIR::InterfaceId cached_import_interface_id_;
  725. };
  726. } // namespace
  727. struct CandidateImpl {
  728. SemIR::ImplId impl_id;
  729. SemIR::InstId loc_inst_id;
  730. // Used for sorting the candidates to find the most-specialized match.
  731. TypeStructure type_structure;
  732. };
  733. struct CandidateImpls {
  734. llvm::SmallVector<CandidateImpl> impls;
  735. bool consider_cpp_candidates = false;
  736. };
  737. // Returns the list of candidates impls for lookup to select from.
  738. static auto CollectCandidateImplsForQuery(
  739. Context& context, bool final_only, SemIR::ConstantId query_self_const_id,
  740. const TypeStructure& query_type_structure,
  741. SemIR::SpecificInterface& query_specific_interface) -> CandidateImpls {
  742. CandidateImpls candidates;
  743. auto import_irs = FindAssociatedImportIRs(context, query_self_const_id,
  744. query_specific_interface);
  745. for (auto import_ir_id : import_irs) {
  746. // If `Cpp` is an associated package, then we'll instead look for C++
  747. // operator overloads for certain well-known interfaces.
  748. if (import_ir_id == SemIR::ImportIRId::Cpp) {
  749. candidates.consider_cpp_candidates = true;
  750. continue;
  751. }
  752. // Instead of importing all impls, only import ones that are in some way
  753. // connected to this query.
  754. ImportImplFilter filter(context, import_ir_id, query_specific_interface);
  755. for (auto [import_impl_id, _] :
  756. context.import_irs().Get(import_ir_id).sem_ir->impls().enumerate()) {
  757. if (filter.IsRelevantImpl(import_impl_id)) {
  758. // TODO: Track the relevant impls and only consider those ones and any
  759. // local impls, rather than looping over all impls below.
  760. ImportImpl(context, import_ir_id, import_impl_id);
  761. }
  762. }
  763. }
  764. for (auto [id, impl] : context.impls().enumerate()) {
  765. CARBON_CHECK(impl.witness_id.has_value());
  766. if (final_only && !IsImplEffectivelyFinal(context, impl)) {
  767. continue;
  768. }
  769. // If the impl's interface_id differs from the query, then this impl can
  770. // not possibly provide the queried interface.
  771. if (impl.interface.interface_id != query_specific_interface.interface_id) {
  772. continue;
  773. }
  774. // When the impl's interface_id matches, but the interface is generic, the
  775. // impl may or may not match based on restrictions in the generic
  776. // parameters of the impl.
  777. //
  778. // As a shortcut, if the impl's constraint is not symbolic (does not
  779. // depend on any generic parameters), then we can determine whether we match
  780. // by looking if the specific ids match exactly.
  781. auto impl_interface_const_id =
  782. context.constant_values().Get(impl.constraint_id);
  783. if (!impl_interface_const_id.is_symbolic() &&
  784. impl.interface.specific_id != query_specific_interface.specific_id) {
  785. continue;
  786. }
  787. // Build the type structure used for choosing the best the candidate.
  788. auto type_structure =
  789. BuildTypeStructure(context, impl.self_id, impl.interface);
  790. if (!type_structure) {
  791. continue;
  792. }
  793. // TODO: We can skip the comparison here if the `impl_interface_const_id` is
  794. // not symbolic, since when the interface and specific ids match, and they
  795. // aren't symbolic, the structure will be identical.
  796. if (!query_type_structure.CompareStructure(
  797. TypeStructure::CompareTest::IsEqualToOrMoreSpecificThan,
  798. *type_structure)) {
  799. continue;
  800. }
  801. candidates.impls.push_back(
  802. {id, impl.definition_id, std::move(*type_structure)});
  803. }
  804. auto compare = [](auto& lhs, auto& rhs) -> bool {
  805. return lhs.type_structure < rhs.type_structure;
  806. };
  807. // Stable sort is used so that impls that are seen first are preferred when
  808. // they have an equal priority ordering.
  809. // TODO: Allow Carbon code to provide a priority ordering explicitly. For
  810. // now they have all the same priority, so the priority is the order in
  811. // which they are found in code.
  812. llvm::stable_sort(candidates.impls, compare);
  813. return candidates;
  814. }
  815. // Given a value whose type `IsFacetTypeOrError`, returns the corresponding
  816. // type.
  817. static auto GetFacetAsType(Context& context, SemIR::LocId loc_id,
  818. SemIR::ConstantId facet_or_type_const_id)
  819. -> SemIR::TypeId {
  820. auto facet_or_type_id =
  821. context.constant_values().GetInstId(facet_or_type_const_id);
  822. auto type_type_id = context.insts().Get(facet_or_type_id).type_id();
  823. CARBON_CHECK(context.types().IsFacetTypeOrError(type_type_id));
  824. if (context.types().Is<SemIR::FacetType>(type_type_id)) {
  825. // It's a facet; access its type.
  826. facet_or_type_id = GetOrAddInst<SemIR::FacetAccessType>(
  827. context, loc_id,
  828. {.type_id = SemIR::TypeType::TypeId,
  829. .facet_value_inst_id = facet_or_type_id});
  830. }
  831. return context.types().GetTypeIdForTypeInstId(facet_or_type_id);
  832. }
  833. auto EvalLookupSingleImplWitness(Context& context, SemIR::LocId loc_id,
  834. SemIR::LookupImplWitness eval_query,
  835. SemIR::InstId self_facet_value_inst_id,
  836. bool poison_final_results)
  837. -> EvalImplLookupResult {
  838. auto query_specific_interface =
  839. context.specific_interfaces().Get(eval_query.query_specific_interface_id);
  840. auto facet_lookup_result = LookupImplWitnessInSelfFacetValue(
  841. context, loc_id, self_facet_value_inst_id, query_specific_interface);
  842. if (facet_lookup_result.has_final_value()) {
  843. return facet_lookup_result;
  844. }
  845. // Ensure specifics don't substitute in weird things for the query self.
  846. CARBON_CHECK(context.types().IsFacetType(
  847. context.insts().Get(eval_query.query_self_inst_id).type_id()));
  848. SemIR::ConstantId query_self_const_id =
  849. context.constant_values().Get(eval_query.query_self_inst_id);
  850. // The kind of lookup we're performing, which determines what kind of result
  851. // we provide.
  852. enum LookupKind {
  853. // This is a concrete query, which should either provide a concrete witness
  854. // or fail.
  855. Concrete,
  856. // This query refers to an interface that can be found symbolically within
  857. // the facet type of the self value. The lookup will always succeed, but we
  858. // are still checking in case a more precise final impl supplies values of
  859. // associated constants.
  860. FoundInFacet,
  861. // This is an impl lookup with a symbolic query.
  862. Symbolic,
  863. };
  864. LookupKind kind =
  865. QueryIsConcrete(context, query_self_const_id, query_specific_interface)
  866. ? Concrete
  867. : facet_lookup_result.has_value() ? FoundInFacet
  868. : Symbolic;
  869. CARBON_CHECK(kind != Concrete || !facet_lookup_result.has_value(),
  870. "Non-concrete facet lookup value for concrete query");
  871. // If the self type is a facet that provides a witness, then we are in an
  872. // `interface` or an `impl`. In both cases, we don't want to do any impl
  873. // lookups. The query will eventually resolve to a concrete witness when it
  874. // can get it from the self facet value, when it has a specific applied in the
  875. // future.
  876. //
  877. // In particular, this avoids a LookupImplWitness instruction in the eval
  878. // block of an impl declaration from doing impl lookup. Specifically the
  879. // lookup of the implicit .Self in `impl ... where .X`. If it does impl lookup
  880. // when the eval block is run, it finds the same `impl`, tries to build a
  881. // specific from it, which runs the eval block, creating a recursive loop that
  882. // crashes.
  883. if (kind == FoundInFacet) {
  884. if (auto bind = context.insts().TryGetAs<SemIR::SymbolicBinding>(
  885. eval_query.query_self_inst_id)) {
  886. const auto& entity = context.entity_names().Get(bind->entity_name_id);
  887. if (entity.name_id == SemIR::NameId::PeriodSelf ||
  888. entity.name_id == SemIR::NameId::SelfType) {
  889. return EvalImplLookupResult::MakeNonFinal();
  890. }
  891. }
  892. }
  893. auto query_type_structure = BuildTypeStructure(
  894. context, context.constant_values().GetInstId(query_self_const_id),
  895. query_specific_interface);
  896. if (!query_type_structure) {
  897. return EvalImplLookupResult::MakeNone();
  898. }
  899. // If the self value is a (symbolic) facet value that has a symbolic witness,
  900. // then we don't need to do impl lookup, except that we want to find any final
  901. // impls to return a concrete witness if possible. So we limit the query to
  902. // final impls only in that case. Note as in the CHECK above, the query can
  903. // not be concrete in this case, so only final impls can produce a concrete
  904. // witness for this query.
  905. auto candidates = CollectCandidateImplsForQuery(
  906. context, kind == FoundInFacet, query_self_const_id, *query_type_structure,
  907. query_specific_interface);
  908. for (const auto& candidate : candidates.impls) {
  909. // In deferred lookup for a symbolic impl witness, while building a
  910. // specific, there may be no stack yet as this may be the first lookup. If
  911. // further lookups are started as a result in deduce, they will build the
  912. // stack.
  913. //
  914. // NOTE: Don't retain a reference into the stack, it may be invalidated if
  915. // we do further impl lookups when GetWitnessIdForImpl() does deduction.
  916. if (!context.impl_lookup_stack().empty()) {
  917. context.impl_lookup_stack().back().impl_loc = candidate.loc_inst_id;
  918. }
  919. auto result = GetWitnessIdForImpl(
  920. context, loc_id, kind == Concrete, query_self_const_id,
  921. query_specific_interface, candidate.impl_id);
  922. if (result.has_value()) {
  923. // Record the query which found a final impl witness. It's illegal to
  924. // write a final impl afterward that would match the same query.
  925. //
  926. // If the impl was effectively final, then we don't need to poison here. A
  927. // change of query result will already be diagnosed at the point where the
  928. // new impl decl was written that changes the result.
  929. if (poison_final_results && result.has_final_value() &&
  930. !IsImplEffectivelyFinal(context,
  931. context.impls().Get(candidate.impl_id))) {
  932. context.poisoned_concrete_impl_lookup_queries().push_back(
  933. {.loc_id = loc_id,
  934. .query = eval_query,
  935. .impl_witness = result.final_witness()});
  936. }
  937. if (kind == Concrete && candidates.consider_cpp_candidates) {
  938. // We found a Carbon impl. Also check for a C++ candidate that is a
  939. // better match than that impl.
  940. auto cpp_witness_id = LookupCppImpl(
  941. context, loc_id,
  942. GetFacetAsType(context, loc_id, query_self_const_id),
  943. query_specific_interface, &candidate.type_structure,
  944. SemIR::LocId(
  945. context.impls().Get(candidate.impl_id).first_owning_decl_id));
  946. if (cpp_witness_id.has_value()) {
  947. return EvalImplLookupResult::MakeFinal(cpp_witness_id);
  948. }
  949. }
  950. return result;
  951. }
  952. }
  953. // We didn't find a matching impl. Produce a suitable result.
  954. switch (kind) {
  955. case Concrete:
  956. if (candidates.consider_cpp_candidates) {
  957. // Look for a matching C++ result, with no Carbon candidate to compare
  958. // against.
  959. auto cpp_witness_id = LookupCppImpl(
  960. context, loc_id,
  961. GetFacetAsType(context, loc_id, query_self_const_id),
  962. query_specific_interface, nullptr, SemIR::LocId::None);
  963. if (cpp_witness_id.has_value()) {
  964. return EvalImplLookupResult::MakeFinal(cpp_witness_id);
  965. }
  966. }
  967. return EvalImplLookupResult::MakeNone();
  968. case FoundInFacet:
  969. // We did not find a final impl, but the self value is a facet that
  970. // provides a symbolic witness. Record that an impl will exist for the
  971. // specific, but is yet unknown.
  972. return EvalImplLookupResult::MakeNonFinal();
  973. case Symbolic:
  974. return EvalImplLookupResult::MakeNone();
  975. }
  976. }
  977. auto LookupMatchesImpl(Context& context, SemIR::LocId loc_id,
  978. SemIR::ConstantId query_self_const_id,
  979. SemIR::SpecificInterface query_specific_interface,
  980. SemIR::ImplId target_impl) -> bool {
  981. if (query_self_const_id == SemIR::ErrorInst::ConstantId) {
  982. return false;
  983. }
  984. auto result = GetWitnessIdForImpl(
  985. context, loc_id, /*query_is_concrete=*/false, query_self_const_id,
  986. query_specific_interface, target_impl);
  987. return result.has_value();
  988. }
  989. } // namespace Carbon::Check