deduce.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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/deduce.h"
  5. #include "llvm/ADT/SmallBitVector.h"
  6. #include "toolchain/base/kind_switch.h"
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/check/convert.h"
  9. #include "toolchain/check/generic.h"
  10. #include "toolchain/check/subst.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. #include "toolchain/sem_ir/impl.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. namespace Carbon::Check {
  15. namespace {
  16. // A list of pairs of (instruction from generic, corresponding instruction from
  17. // call to of generic) for which we still need to perform deduction, along with
  18. // methods to add and pop pending deductions from the list. Deductions are
  19. // popped in order from most- to least-recently pushed, with the intent that
  20. // they are visited in depth-first order, although the order is not expected to
  21. // matter except when it influences which error is diagnosed.
  22. class DeductionWorklist {
  23. public:
  24. explicit DeductionWorklist(Context& context) : context_(context) {}
  25. struct PendingDeduction {
  26. SemIR::InstId param;
  27. SemIR::InstId arg;
  28. bool needs_substitution;
  29. };
  30. // Adds a single (param, arg) deduction.
  31. auto Add(SemIR::InstId param, SemIR::InstId arg, bool needs_substitution)
  32. -> void {
  33. deductions_.push_back(
  34. {.param = param, .arg = arg, .needs_substitution = needs_substitution});
  35. }
  36. // Adds a single (param, arg) type deduction.
  37. auto Add(SemIR::TypeId param, SemIR::TypeId arg, bool needs_substitution)
  38. -> void {
  39. Add(context_.types().GetInstId(param), context_.types().GetInstId(arg),
  40. needs_substitution);
  41. }
  42. // Adds a single (param, arg) deduction of a specific.
  43. auto Add(SemIR::SpecificId param, SemIR::SpecificId arg,
  44. bool needs_substitution) -> void {
  45. if (!param.has_value() || !arg.has_value()) {
  46. return;
  47. }
  48. auto& param_specific = context_.specifics().Get(param);
  49. auto& arg_specific = context_.specifics().Get(arg);
  50. if (param_specific.generic_id != arg_specific.generic_id) {
  51. // TODO: Decide whether to error on this or just treat the specific as
  52. // non-deduced. For now we treat it as non-deduced.
  53. return;
  54. }
  55. AddAll(param_specific.args_id, arg_specific.args_id, needs_substitution);
  56. }
  57. // Adds a list of (param, arg) deductions. These are added in reverse order so
  58. // they are popped in forward order.
  59. template <typename ElementId>
  60. auto AddAll(llvm::ArrayRef<ElementId> params, llvm::ArrayRef<ElementId> args,
  61. bool needs_substitution) -> void {
  62. if (params.size() != args.size()) {
  63. // TODO: Decide whether to error on this or just treat the parameter list
  64. // as non-deduced. For now we treat it as non-deduced.
  65. return;
  66. }
  67. for (auto [param, arg] : llvm::reverse(llvm::zip_equal(params, args))) {
  68. Add(param, arg, needs_substitution);
  69. }
  70. }
  71. auto AddAll(SemIR::InstBlockId params, llvm::ArrayRef<SemIR::InstId> args,
  72. bool needs_substitution) -> void {
  73. AddAll(context_.inst_blocks().Get(params), args, needs_substitution);
  74. }
  75. auto AddAll(SemIR::StructTypeFieldsId params, SemIR::StructTypeFieldsId args,
  76. bool needs_substitution) -> void {
  77. const auto& param_fields = context_.struct_type_fields().Get(params);
  78. const auto& arg_fields = context_.struct_type_fields().Get(args);
  79. if (param_fields.size() != arg_fields.size()) {
  80. // TODO: Decide whether to error on this or just treat the parameter list
  81. // as non-deduced. For now we treat it as non-deduced.
  82. return;
  83. }
  84. // Don't do deduction unless the names match in order.
  85. // TODO: Support reordering of names.
  86. for (auto [param, arg] : llvm::zip_equal(param_fields, arg_fields)) {
  87. if (param.name_id != arg.name_id) {
  88. return;
  89. }
  90. }
  91. for (auto [param, arg] :
  92. llvm::reverse(llvm::zip_equal(param_fields, arg_fields))) {
  93. Add(param.type_id, arg.type_id, needs_substitution);
  94. }
  95. }
  96. auto AddAll(SemIR::InstBlockId params, SemIR::InstBlockId args,
  97. bool needs_substitution) -> void {
  98. AddAll(context_.inst_blocks().Get(params), context_.inst_blocks().Get(args),
  99. needs_substitution);
  100. }
  101. auto AddAll(SemIR::TypeBlockId params, SemIR::TypeBlockId args,
  102. bool needs_substitution) -> void {
  103. AddAll(context_.type_blocks().Get(params), context_.type_blocks().Get(args),
  104. needs_substitution);
  105. }
  106. auto AddAll(SemIR::FacetTypeId params, SemIR::FacetTypeId args,
  107. bool needs_substitution) -> void {
  108. const auto& param_impls =
  109. context_.facet_types().Get(params).impls_constraints;
  110. const auto& arg_impls = context_.facet_types().Get(args).impls_constraints;
  111. // TODO: Decide whether to error on these or just treat the parameter list
  112. // as non-deduced. For now we treat it as non-deduced.
  113. if (param_impls.size() != 1 || arg_impls.size() != 1) {
  114. return;
  115. }
  116. auto param = param_impls.front();
  117. auto arg = arg_impls.front();
  118. if (param.interface_id != arg.interface_id) {
  119. return;
  120. }
  121. Add(param.specific_id, arg.specific_id, needs_substitution);
  122. }
  123. // Adds a (param, arg) pair for an instruction argument, given its kind.
  124. auto AddInstArg(SemIR::IdKind kind, int32_t param, int32_t arg,
  125. bool needs_substitution) -> void {
  126. switch (kind) {
  127. case SemIR::IdKind::None:
  128. case SemIR::IdKind::For<SemIR::ClassId>:
  129. case SemIR::IdKind::For<SemIR::IntKind>:
  130. break;
  131. case SemIR::IdKind::For<SemIR::InstId>:
  132. Add(SemIR::InstId(param), SemIR::InstId(arg), needs_substitution);
  133. break;
  134. case SemIR::IdKind::For<SemIR::TypeId>:
  135. Add(SemIR::TypeId(param), SemIR::TypeId(arg), needs_substitution);
  136. break;
  137. case SemIR::IdKind::For<SemIR::StructTypeFieldsId>:
  138. AddAll(SemIR::StructTypeFieldsId(param), SemIR::StructTypeFieldsId(arg),
  139. needs_substitution);
  140. break;
  141. case SemIR::IdKind::For<SemIR::InstBlockId>:
  142. AddAll(SemIR::InstBlockId(param), SemIR::InstBlockId(arg),
  143. needs_substitution);
  144. break;
  145. case SemIR::IdKind::For<SemIR::TypeBlockId>:
  146. AddAll(SemIR::TypeBlockId(param), SemIR::TypeBlockId(arg),
  147. needs_substitution);
  148. break;
  149. case SemIR::IdKind::For<SemIR::SpecificId>:
  150. Add(SemIR::SpecificId(param), SemIR::SpecificId(arg),
  151. needs_substitution);
  152. break;
  153. case SemIR::IdKind::For<SemIR::FacetTypeId>:
  154. AddAll(SemIR::FacetTypeId(param), SemIR::FacetTypeId(arg),
  155. needs_substitution);
  156. break;
  157. default:
  158. CARBON_FATAL("unexpected argument kind");
  159. }
  160. }
  161. // Returns whether we have completed all deductions.
  162. auto Done() -> bool { return deductions_.empty(); }
  163. // Pops the next deduction. Requires `!Done()`.
  164. auto PopNext() -> PendingDeduction { return deductions_.pop_back_val(); }
  165. private:
  166. Context& context_;
  167. llvm::SmallVector<PendingDeduction> deductions_;
  168. };
  169. // State that is tracked throughout the deduction process.
  170. class DeductionContext {
  171. public:
  172. // Preparse to perform deduction. If an enclosing specific or self type
  173. // are provided, adds the corresponding arguments as known arguments that will
  174. // not be deduced.
  175. DeductionContext(Context& context, SemIR::LocId loc_id,
  176. SemIR::GenericId generic_id,
  177. SemIR::SpecificId enclosing_specific_id,
  178. SemIR::InstId self_type_id, bool diagnose);
  179. auto context() const -> Context& { return *context_; }
  180. // Adds a pending deduction of `param` from `arg`. `needs_substitution`
  181. // indicates whether we need to substitute known generic parameters into
  182. // `param`.
  183. template <typename ParamT, typename ArgT>
  184. auto Add(ParamT param, ArgT arg, bool needs_substitution) -> void {
  185. worklist_.Add(param, arg, needs_substitution);
  186. }
  187. // Same as `Add` but for an array or block of operands.
  188. template <typename ParamT, typename ArgT>
  189. auto AddAll(ParamT param, ArgT arg, bool needs_substitution) -> void {
  190. worklist_.AddAll(param, arg, needs_substitution);
  191. }
  192. // Performs all deductions in the deduction worklist. Returns whether
  193. // deduction succeeded.
  194. auto Deduce() -> bool;
  195. // Returns whether every generic parameter has a corresponding deduced generic
  196. // argument. If not, issues a suitable diagnostic.
  197. auto CheckDeductionIsComplete() -> bool;
  198. // Forms a specific corresponding to the deduced generic with the deduced
  199. // argument list. Must not be called before deduction is complete.
  200. auto MakeSpecific() -> SemIR::SpecificId;
  201. private:
  202. Context* context_;
  203. SemIR::LocId loc_id_;
  204. SemIR::GenericId generic_id_;
  205. bool diagnose_;
  206. DeductionWorklist worklist_;
  207. llvm::SmallVector<SemIR::InstId> result_arg_ids_;
  208. llvm::SmallVector<Substitution> substitutions_;
  209. SemIR::CompileTimeBindIndex first_deduced_index_;
  210. // Non-deduced indexes, indexed by parameter index - first_deduced_index_.
  211. llvm::SmallBitVector non_deduced_indexes_;
  212. };
  213. } // namespace
  214. static auto NoteGenericHere(Context& context, SemIR::GenericId generic_id,
  215. Context::DiagnosticBuilder& diag) -> void {
  216. CARBON_DIAGNOSTIC(DeductionGenericHere, Note,
  217. "while deducing parameters of generic declared here");
  218. diag.Note(context.generics().Get(generic_id).decl_id, DeductionGenericHere);
  219. }
  220. DeductionContext::DeductionContext(Context& context, SemIR::LocId loc_id,
  221. SemIR::GenericId generic_id,
  222. SemIR::SpecificId enclosing_specific_id,
  223. SemIR::InstId self_type_id, bool diagnose)
  224. : context_(&context),
  225. loc_id_(loc_id),
  226. generic_id_(generic_id),
  227. diagnose_(diagnose),
  228. worklist_(context),
  229. first_deduced_index_(0) {
  230. CARBON_CHECK(generic_id.has_value(),
  231. "Performing deduction for non-generic entity");
  232. // Initialize the deduced arguments to `None`.
  233. result_arg_ids_.resize(
  234. context.inst_blocks()
  235. .Get(context.generics().Get(generic_id_).bindings_id)
  236. .size(),
  237. SemIR::InstId::None);
  238. if (enclosing_specific_id.has_value()) {
  239. // Copy any outer generic arguments from the specified instance and prepare
  240. // to substitute them into the function declaration.
  241. auto args = context.inst_blocks().Get(
  242. context.specifics().Get(enclosing_specific_id).args_id);
  243. llvm::copy(args, result_arg_ids_.begin());
  244. // TODO: Subst is linear in the length of the substitutions list. Change
  245. // it so we can pass in an array mapping indexes to substitutions instead.
  246. substitutions_.reserve(args.size());
  247. for (auto [i, subst_inst_id] : llvm::enumerate(args)) {
  248. substitutions_.push_back(
  249. {.bind_id = SemIR::CompileTimeBindIndex(i),
  250. .replacement_id = context.constant_values().Get(subst_inst_id)});
  251. }
  252. first_deduced_index_ = SemIR::CompileTimeBindIndex(args.size());
  253. }
  254. if (self_type_id.has_value()) {
  255. // Copy the provided `Self` type as the value of the next binding.
  256. auto self_index = first_deduced_index_;
  257. result_arg_ids_[self_index.index] = self_type_id;
  258. substitutions_.push_back(
  259. {.bind_id = SemIR::CompileTimeBindIndex(self_index),
  260. .replacement_id = context.constant_values().Get(self_type_id)});
  261. first_deduced_index_ = SemIR::CompileTimeBindIndex(self_index.index + 1);
  262. }
  263. non_deduced_indexes_.resize(result_arg_ids_.size() -
  264. first_deduced_index_.index);
  265. }
  266. auto DeductionContext::Deduce() -> bool {
  267. while (!worklist_.Done()) {
  268. auto [param_id, arg_id, needs_substitution] = worklist_.PopNext();
  269. auto note_initializing_param = [&](auto& builder) {
  270. if (auto param =
  271. context().insts().TryGetAs<SemIR::SymbolicBindingPattern>(
  272. param_id)) {
  273. CARBON_DIAGNOSTIC(InitializingGenericParam, Note,
  274. "initializing generic parameter `{0}` declared here",
  275. SemIR::NameId);
  276. builder.Note(
  277. param_id, InitializingGenericParam,
  278. context().entity_names().Get(param->entity_name_id).name_id);
  279. } else {
  280. NoteGenericHere(context(), generic_id_, builder);
  281. }
  282. };
  283. // TODO: Bail out if there's nothing to deduce: if we're not in a pattern
  284. // and the parameter doesn't have a symbolic constant value.
  285. // If the parameter has a symbolic type, deduce against that.
  286. auto param_type_id = context().insts().Get(param_id).type_id();
  287. if (param_type_id.AsConstantId().is_symbolic()) {
  288. Add(context().types().GetInstId(param_type_id),
  289. context().types().GetInstId(context().insts().Get(arg_id).type_id()),
  290. needs_substitution);
  291. } else {
  292. // The argument needs to have the same type as the parameter.
  293. // TODO: Suppress diagnostics here if diagnose_ is false.
  294. // TODO: Only do this when deducing against a symbolic pattern.
  295. DiagnosticAnnotationScope annotate_diagnostics(&context().emitter(),
  296. note_initializing_param);
  297. arg_id = ConvertToValueOfType(context(), loc_id_, arg_id, param_type_id);
  298. if (arg_id == SemIR::ErrorInst::SingletonInstId) {
  299. return false;
  300. }
  301. }
  302. // Attempt to match `param_inst` against `arg_id`. If the match succeeds,
  303. // this should `continue` the outer loop. On `break`, we will try to desugar
  304. // the parameter to continue looking for a match.
  305. auto param_inst = context().insts().Get(param_id);
  306. CARBON_KIND_SWITCH(param_inst) {
  307. // Deducing a symbolic binding pattern from an argument deduces the
  308. // binding as having that constant value. For example, deducing
  309. // `(T:! type)` against `(i32)` deduces `T` to be `i32`. This only arises
  310. // when initializing a generic parameter from an explicitly specified
  311. // argument, and in this case, the argument is required to be a
  312. // compile-time constant.
  313. case CARBON_KIND(SemIR::SymbolicBindingPattern bind): {
  314. auto& entity_name = context().entity_names().Get(bind.entity_name_id);
  315. auto index = entity_name.bind_index;
  316. if (!index.has_value()) {
  317. break;
  318. }
  319. CARBON_CHECK(
  320. index >= first_deduced_index_ &&
  321. static_cast<size_t>(index.index) < result_arg_ids_.size(),
  322. "Unexpected index {0} for symbolic binding pattern; "
  323. "expected to be in range [{1}, {2})",
  324. index.index, first_deduced_index_.index, result_arg_ids_.size());
  325. CARBON_CHECK(!result_arg_ids_[index.index].has_value(),
  326. "Deduced a value for parameter prior to its declaration");
  327. auto arg_const_inst_id =
  328. context().constant_values().GetConstantInstId(arg_id);
  329. if (!arg_const_inst_id.has_value()) {
  330. if (diagnose_) {
  331. CARBON_DIAGNOSTIC(CompTimeArgumentNotConstant, Error,
  332. "argument for generic parameter is not a "
  333. "compile-time constant");
  334. auto diag =
  335. context().emitter().Build(loc_id_, CompTimeArgumentNotConstant);
  336. note_initializing_param(diag);
  337. diag.Emit();
  338. }
  339. return false;
  340. }
  341. result_arg_ids_[index.index] = arg_const_inst_id;
  342. // This parameter index should not be deduced if it appears later.
  343. non_deduced_indexes_[index.index - first_deduced_index_.index] = true;
  344. continue;
  345. }
  346. // Deducing a symbolic binding appearing within an expression against a
  347. // constant value deduces the binding as having that value. For example,
  348. // deducing `[T:! type](x: T)` against `("foo")` deduces `T` as `String`.
  349. case CARBON_KIND(SemIR::BindSymbolicName bind): {
  350. auto& entity_name = context().entity_names().Get(bind.entity_name_id);
  351. auto index = entity_name.bind_index;
  352. if (!index.has_value() || index < first_deduced_index_ ||
  353. non_deduced_indexes_[index.index - first_deduced_index_.index]) {
  354. break;
  355. }
  356. CARBON_CHECK(static_cast<size_t>(index.index) < result_arg_ids_.size(),
  357. "Deduced value for unexpected index {0}; expected to "
  358. "deduce {1} arguments.",
  359. index, result_arg_ids_.size());
  360. auto arg_const_inst_id =
  361. context().constant_values().GetConstantInstId(arg_id);
  362. if (arg_const_inst_id.has_value()) {
  363. if (result_arg_ids_[index.index].has_value() &&
  364. result_arg_ids_[index.index] != arg_const_inst_id) {
  365. if (diagnose_) {
  366. // TODO: Include the two different deduced values.
  367. CARBON_DIAGNOSTIC(DeductionInconsistent, Error,
  368. "inconsistent deductions for value of generic "
  369. "parameter `{0}`",
  370. SemIR::NameId);
  371. auto diag = context().emitter().Build(
  372. loc_id_, DeductionInconsistent, entity_name.name_id);
  373. NoteGenericHere(context(), generic_id_, diag);
  374. diag.Emit();
  375. }
  376. return false;
  377. }
  378. result_arg_ids_[index.index] = arg_const_inst_id;
  379. }
  380. continue;
  381. }
  382. case CARBON_KIND(SemIR::ValueParamPattern pattern): {
  383. Add(pattern.subpattern_id, arg_id, needs_substitution);
  384. continue;
  385. }
  386. case SemIR::StructValue::Kind:
  387. // TODO: Match field name order between param and arg.
  388. break;
  389. // TODO: Handle more cases.
  390. default:
  391. if (param_inst.kind().deduce_through()) {
  392. // Various kinds of parameter should match an argument of the same
  393. // form, if the operands all match.
  394. auto arg_inst = context().insts().Get(arg_id);
  395. if (arg_inst.kind() != param_inst.kind()) {
  396. break;
  397. }
  398. auto [kind0, kind1] = param_inst.ArgKinds();
  399. worklist_.AddInstArg(kind0, param_inst.arg0(), arg_inst.arg0(),
  400. needs_substitution);
  401. worklist_.AddInstArg(kind1, param_inst.arg1(), arg_inst.arg1(),
  402. needs_substitution);
  403. continue;
  404. }
  405. break;
  406. }
  407. // We didn't manage to deduce against the syntactic form of the parameter.
  408. // Convert it to a canonical constant value and try deducing against that.
  409. auto param_const_id = context().constant_values().Get(param_id);
  410. if (!param_const_id.has_value() || !param_const_id.is_symbolic()) {
  411. // It's not a symbolic constant. There's nothing here to deduce.
  412. continue;
  413. }
  414. auto param_const_inst_id =
  415. context().constant_values().GetInstId(param_const_id);
  416. if (param_const_inst_id != param_id) {
  417. Add(param_const_inst_id, arg_id, needs_substitution);
  418. continue;
  419. }
  420. // If we've not yet substituted into the parameter, do so now and try again.
  421. if (needs_substitution) {
  422. param_const_id = SubstConstant(context(), param_const_id, substitutions_);
  423. if (!param_const_id.has_value() || !param_const_id.is_symbolic()) {
  424. continue;
  425. }
  426. Add(context().constant_values().GetInstId(param_const_id), arg_id,
  427. /*needs_substitution=*/false);
  428. }
  429. }
  430. return true;
  431. }
  432. auto DeductionContext::CheckDeductionIsComplete() -> bool {
  433. // Check we deduced an argument value for every parameter.
  434. for (auto [i, deduced_arg_id] :
  435. llvm::enumerate(llvm::ArrayRef(result_arg_ids_)
  436. .drop_front(first_deduced_index_.index))) {
  437. if (!deduced_arg_id.has_value()) {
  438. if (diagnose_) {
  439. auto binding_index = first_deduced_index_.index + i;
  440. auto binding_id = context().inst_blocks().Get(
  441. context().generics().Get(generic_id_).bindings_id)[binding_index];
  442. auto entity_name_id = context()
  443. .insts()
  444. .GetAs<SemIR::AnyBindName>(binding_id)
  445. .entity_name_id;
  446. CARBON_DIAGNOSTIC(DeductionIncomplete, Error,
  447. "cannot deduce value for generic parameter `{0}`",
  448. SemIR::NameId);
  449. auto diag = context().emitter().Build(
  450. loc_id_, DeductionIncomplete,
  451. context().entity_names().Get(entity_name_id).name_id);
  452. NoteGenericHere(context(), generic_id_, diag);
  453. diag.Emit();
  454. }
  455. return false;
  456. }
  457. }
  458. return true;
  459. }
  460. auto DeductionContext::MakeSpecific() -> SemIR::SpecificId {
  461. // TODO: Convert the deduced values to the types of the bindings.
  462. return Check::MakeSpecific(context(), loc_id_, generic_id_, result_arg_ids_);
  463. }
  464. auto DeduceGenericCallArguments(
  465. Context& context, SemIR::LocId loc_id, SemIR::GenericId generic_id,
  466. SemIR::SpecificId enclosing_specific_id, SemIR::InstId self_type_id,
  467. [[maybe_unused]] SemIR::InstBlockId implicit_params_id,
  468. SemIR::InstBlockId params_id, [[maybe_unused]] SemIR::InstId self_id,
  469. llvm::ArrayRef<SemIR::InstId> arg_ids) -> SemIR::SpecificId {
  470. DeductionContext deduction(context, loc_id, generic_id, enclosing_specific_id,
  471. self_type_id, /*diagnose=*/true);
  472. // Prepare to perform deduction of the explicit parameters against their
  473. // arguments.
  474. // TODO: Also perform deduction for type of self.
  475. deduction.AddAll(params_id, arg_ids, /*needs_substitution=*/true);
  476. if (!deduction.Deduce() || !deduction.CheckDeductionIsComplete()) {
  477. return SemIR::SpecificId::None;
  478. }
  479. return deduction.MakeSpecific();
  480. }
  481. // Deduces the impl arguments to use in a use of a parameterized impl. Returns
  482. // `None` if deduction fails.
  483. auto DeduceImplArguments(Context& context, SemIR::LocId loc_id,
  484. const SemIR::Impl& impl, SemIR::ConstantId self_id,
  485. SemIR::ConstantId constraint_id) -> SemIR::SpecificId {
  486. DeductionContext deduction(context, loc_id, impl.generic_id,
  487. /*enclosing_specific_id=*/SemIR::SpecificId::None,
  488. /*self_type_id=*/SemIR::InstId::None,
  489. /*diagnose=*/false);
  490. // Prepare to perform deduction of the type and interface.
  491. deduction.Add(impl.self_id, context.constant_values().GetInstId(self_id),
  492. /*needs_substitution=*/false);
  493. deduction.Add(impl.constraint_id,
  494. context.constant_values().GetInstId(constraint_id),
  495. /*needs_substitution=*/false);
  496. if (!deduction.Deduce() || !deduction.CheckDeductionIsComplete()) {
  497. return SemIR::SpecificId::None;
  498. }
  499. return deduction.MakeSpecific();
  500. }
  501. } // namespace Carbon::Check