deduce.cpp 22 KB

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