generic.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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/generic.h"
  5. #include "common/map.h"
  6. #include "toolchain/base/kind_switch.h"
  7. #include "toolchain/check/eval.h"
  8. #include "toolchain/check/generic_region_stack.h"
  9. #include "toolchain/check/subst.h"
  10. #include "toolchain/check/type_completion.h"
  11. #include "toolchain/sem_ir/generic.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. #include "toolchain/sem_ir/inst.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. static auto MakeSelfSpecificId(Context& context, SemIR::GenericId generic_id)
  17. -> SemIR::SpecificId;
  18. static auto ResolveSpecificDeclaration(Context& context, SemIRLoc loc,
  19. SemIR::SpecificId specific_id) -> void;
  20. auto StartGenericDecl(Context& context) -> void {
  21. context.generic_region_stack().Push();
  22. }
  23. auto StartGenericDefinition(Context& context) -> void {
  24. // Push a generic region even if we don't have a generic_id. We might still
  25. // have locally-introduced generic parameters to track:
  26. //
  27. // fn F() {
  28. // let T:! type = i32;
  29. // var x: T;
  30. // }
  31. context.generic_region_stack().Push();
  32. }
  33. // Adds an instruction `generic_inst_id` to the eval block for a generic region,
  34. // which is the current instruction block. The instruction `generic_inst_id` is
  35. // expected to compute the value of the constant described by `const_inst_id` in
  36. // each specific. Forms and returns a corresponding symbolic constant ID that
  37. // refers to the substituted value of that instruction in each specific.
  38. static auto AddGenericConstantInstToEvalBlock(
  39. Context& context, SemIR::GenericId generic_id,
  40. SemIR::GenericInstIndex::Region region, SemIR::InstId const_inst_id,
  41. SemIR::InstId generic_inst_id) -> SemIR::ConstantId {
  42. auto index = SemIR::GenericInstIndex(
  43. region, context.inst_block_stack().PeekCurrentBlockContents().size());
  44. context.inst_block_stack().AddInstId(generic_inst_id);
  45. return context.constant_values().AddSymbolicConstant(
  46. {.inst_id = const_inst_id, .generic_id = generic_id, .index = index});
  47. }
  48. namespace {
  49. // A map from an instruction ID representing a canonical symbolic constant to an
  50. // instruction within an eval block of the generic that computes the specific
  51. // value for that constant.
  52. //
  53. // We arbitrarily use a small size of 256 bytes for the map.
  54. // TODO: Determine a better number based on measurements.
  55. using ConstantsInGenericMap = Map<SemIR::InstId, SemIR::InstId, 256>;
  56. // Substitution callbacks to rebuild a generic constant in the eval block for a
  57. // generic region.
  58. class RebuildGenericConstantInEvalBlockCallbacks final
  59. : public SubstInstCallbacks {
  60. public:
  61. RebuildGenericConstantInEvalBlockCallbacks(
  62. Context& context, SemIR::GenericId generic_id,
  63. SemIR::GenericInstIndex::Region region, SemIR::LocId loc_id,
  64. ConstantsInGenericMap& constants_in_generic)
  65. : context_(context),
  66. generic_id_(generic_id),
  67. region_(region),
  68. loc_id_(loc_id),
  69. constants_in_generic_(constants_in_generic) {}
  70. // Check for instructions for which we already have a mapping into the eval
  71. // block, and substitute them for the instructions in the eval block.
  72. auto Subst(SemIR::InstId& inst_id) const -> bool override {
  73. auto const_id = context_.constant_values().Get(inst_id);
  74. if (!const_id.has_value()) {
  75. // An unloaded import ref should never contain anything we need to
  76. // substitute into. Don't trigger loading it here.
  77. CARBON_CHECK(
  78. context_.insts().Is<SemIR::ImportRefUnloaded>(inst_id),
  79. "Substituting into instruction with invalid constant ID: {0}",
  80. context_.insts().Get(inst_id));
  81. return true;
  82. }
  83. if (!context_.constant_values().DependsOnGenericParameter(const_id)) {
  84. // This instruction doesn't have a symbolic constant value, so can't
  85. // contain any bindings that need to be substituted.
  86. return true;
  87. }
  88. // If this instruction is in the map, return the known result.
  89. if (auto result = constants_in_generic_.Lookup(
  90. context_.constant_values().GetInstId(const_id))) {
  91. // In order to reuse instructions from the generic as often as possible,
  92. // keep this instruction as-is if it already has the desired symbolic
  93. // constant value.
  94. if (const_id != context_.constant_values().Get(result.value())) {
  95. inst_id = result.value();
  96. }
  97. CARBON_CHECK(inst_id.has_value());
  98. return true;
  99. }
  100. // If the instruction is a symbolic binding, build a version in the eval
  101. // block.
  102. if (auto binding =
  103. context_.insts().TryGetAs<SemIR::BindSymbolicName>(inst_id)) {
  104. if (context_.entity_names()
  105. .Get(binding->entity_name_id)
  106. .bind_index.has_value()) {
  107. inst_id = Rebuild(inst_id, *binding);
  108. return true;
  109. }
  110. }
  111. if (auto pattern =
  112. context_.insts().TryGetAs<SemIR::SymbolicBindingPattern>(inst_id)) {
  113. inst_id = Rebuild(inst_id, *pattern);
  114. return true;
  115. }
  116. return false;
  117. }
  118. // Build a new instruction in the eval block corresponding to the given
  119. // constant.
  120. auto Rebuild(SemIR::InstId orig_inst_id, SemIR::Inst new_inst) const
  121. -> SemIR::InstId override {
  122. auto const_inst_id =
  123. context_.constant_values().GetConstantInstId(orig_inst_id);
  124. // We might already have an instruction in the eval block if a transitive
  125. // operand of this instruction has the same constant value.
  126. auto result = constants_in_generic_.Insert(const_inst_id, [&] {
  127. // TODO: Add a function on `Context` to add the instruction without
  128. // inserting it into the dependent instructions list or computing a
  129. // constant value for it.
  130. // TODO: Is the location we pick here always appropriate for the new
  131. // instruction?
  132. auto inst_id = context_.sem_ir().insts().AddInNoBlock(
  133. SemIR::LocIdAndInst::UncheckedLoc(loc_id_, new_inst));
  134. auto const_id = AddGenericConstantInstToEvalBlock(
  135. context_, generic_id_, region_, const_inst_id, inst_id);
  136. context_.constant_values().Set(inst_id, const_id);
  137. return inst_id;
  138. });
  139. return result.value();
  140. }
  141. private:
  142. Context& context_;
  143. SemIR::GenericId generic_id_;
  144. SemIR::GenericInstIndex::Region region_;
  145. SemIR::LocId loc_id_;
  146. ConstantsInGenericMap& constants_in_generic_;
  147. };
  148. } // namespace
  149. // Adds instructions to compute the substituted version of `type_id` in each
  150. // specific into the eval block for the generic, which is the current
  151. // instruction block. Returns a symbolic type ID that refers to the substituted
  152. // type in each specific.
  153. static auto AddGenericTypeToEvalBlock(
  154. Context& context, SemIR::GenericId generic_id,
  155. SemIR::GenericInstIndex::Region region, SemIR::LocId loc_id,
  156. ConstantsInGenericMap& constants_in_generic, SemIR::TypeId type_id)
  157. -> SemIR::TypeId {
  158. // Substitute into the type's constant instruction and rebuild it in the eval
  159. // block.
  160. auto type_inst_id =
  161. SubstInst(context, context.types().GetInstId(type_id),
  162. RebuildGenericConstantInEvalBlockCallbacks(
  163. context, generic_id, region, loc_id, constants_in_generic));
  164. return context.GetTypeIdForTypeInst(type_inst_id);
  165. }
  166. // Adds instructions to compute the substituted value of `inst_id` in each
  167. // specific into the eval block for the generic, which is the current
  168. // instruction block. Returns a symbolic constant instruction ID that refers to
  169. // the substituted constant value in each specific.
  170. static auto AddGenericConstantToEvalBlock(
  171. Context& context, SemIR::GenericId generic_id,
  172. SemIR::GenericInstIndex::Region region,
  173. ConstantsInGenericMap& constants_in_generic, SemIR::InstId inst_id)
  174. -> SemIR::ConstantId {
  175. // Substitute into the constant value and rebuild it in the eval block if
  176. // we've not encountered it before.
  177. auto const_inst_id = context.constant_values().GetConstantInstId(inst_id);
  178. auto new_inst_id =
  179. SubstInst(context, const_inst_id,
  180. RebuildGenericConstantInEvalBlockCallbacks(
  181. context, generic_id, region,
  182. context.insts().GetLocId(inst_id), constants_in_generic));
  183. CARBON_CHECK(new_inst_id != const_inst_id,
  184. "Did not apply any substitutions to symbolic constant {0}",
  185. context.insts().Get(const_inst_id));
  186. return context.constant_values().Get(new_inst_id);
  187. }
  188. // Populates a map of constants in a generic from the constants in the
  189. // declaration region, in preparation for building the definition region.
  190. static auto PopulateConstantsFromDeclaration(
  191. Context& context, SemIR::GenericId generic_id,
  192. ConstantsInGenericMap& constants_in_generic) {
  193. // For the definition region, populate constants from the declaration.
  194. auto decl_eval_block = context.inst_blocks().Get(
  195. context.generics().Get(generic_id).decl_block_id);
  196. constants_in_generic.GrowForInsertCount(decl_eval_block.size());
  197. for (auto inst_id : decl_eval_block) {
  198. auto const_inst_id = context.constant_values().GetConstantInstId(inst_id);
  199. auto result = constants_in_generic.Insert(const_inst_id, inst_id);
  200. CARBON_CHECK(result.is_inserted(),
  201. "Duplicate constant in generic decl eval block: {0}",
  202. context.insts().Get(const_inst_id));
  203. }
  204. }
  205. // Builds and returns a block of instructions whose constant values need to be
  206. // evaluated in order to resolve a generic to a specific.
  207. static auto MakeGenericEvalBlock(Context& context, SemIR::GenericId generic_id,
  208. SemIR::GenericInstIndex::Region region)
  209. -> SemIR::InstBlockId {
  210. context.inst_block_stack().Push();
  211. ConstantsInGenericMap constants_in_generic;
  212. // For the definition region, populate constants from the declaration.
  213. if (region == SemIR::GenericInstIndex::Region::Definition) {
  214. PopulateConstantsFromDeclaration(context, generic_id, constants_in_generic);
  215. }
  216. // The work done in this loop might invalidate iterators into the generic
  217. // region stack, but shouldn't add new dependent instructions to the current
  218. // region.
  219. auto num_dependent_insts =
  220. context.generic_region_stack().PeekDependentInsts().size();
  221. for (auto i : llvm::seq(num_dependent_insts)) {
  222. auto [inst_id, dep_kind] =
  223. context.generic_region_stack().PeekDependentInsts()[i];
  224. // If the type is symbolic, replace it with a type specific to this generic.
  225. if ((dep_kind & GenericRegionStack::DependencyKind::SymbolicType) !=
  226. GenericRegionStack::DependencyKind::None) {
  227. auto inst = context.insts().Get(inst_id);
  228. auto type_id = AddGenericTypeToEvalBlock(
  229. context, generic_id, region, context.insts().GetLocId(inst_id),
  230. constants_in_generic, inst.type_id());
  231. // TODO: Eventually, completeness requirements should be modeled as
  232. // constraints on the generic rather than properties of the type. For now,
  233. // require the transformed type to be complete if the original was.
  234. if (context.types().IsComplete(inst.type_id())) {
  235. CompleteTypeOrCheckFail(context, type_id);
  236. }
  237. inst.SetType(type_id);
  238. context.sem_ir().insts().Set(inst_id, inst);
  239. }
  240. // If the instruction has a symbolic constant value, then make a note that
  241. // we'll need to evaluate this instruction when forming the specific. Update
  242. // the constant value of the instruction to refer to the result of that
  243. // eventual evaluation.
  244. if ((dep_kind & GenericRegionStack::DependencyKind::SymbolicConstant) !=
  245. GenericRegionStack::DependencyKind::None) {
  246. // Update the constant value to refer to this generic.
  247. context.constant_values().Set(
  248. inst_id,
  249. AddGenericConstantToEvalBlock(context, generic_id, region,
  250. constants_in_generic, inst_id));
  251. }
  252. }
  253. CARBON_CHECK(
  254. num_dependent_insts ==
  255. context.generic_region_stack().PeekDependentInsts().size(),
  256. "Building eval block added new dependent insts, for example {0}",
  257. context.insts().Get(context.generic_region_stack()
  258. .PeekDependentInsts()[num_dependent_insts]
  259. .inst_id));
  260. return context.inst_block_stack().Pop();
  261. }
  262. // Builds and returns an eval block, given the list of canonical symbolic
  263. // constants that the instructions in the eval block should produce. This is
  264. // used when importing a generic.
  265. auto RebuildGenericEvalBlock(Context& context, SemIR::GenericId generic_id,
  266. SemIR::GenericInstIndex::Region region,
  267. llvm::ArrayRef<SemIR::InstId> const_ids)
  268. -> SemIR::InstBlockId {
  269. context.inst_block_stack().Push();
  270. ConstantsInGenericMap constants_in_generic;
  271. // For the definition region, populate constants from the declaration.
  272. if (region == SemIR::GenericInstIndex::Region::Definition) {
  273. PopulateConstantsFromDeclaration(context, generic_id, constants_in_generic);
  274. }
  275. constants_in_generic.GrowForInsertCount(const_ids.size());
  276. for (auto [i, inst_id] : llvm::enumerate(const_ids)) {
  277. // Build a constant in the inst block.
  278. AddGenericConstantToEvalBlock(context, generic_id, region,
  279. constants_in_generic, inst_id);
  280. CARBON_CHECK(
  281. context.inst_block_stack().PeekCurrentBlockContents().size() == i + 1,
  282. "Produced {0} instructions when importing {1}",
  283. (context.inst_block_stack().PeekCurrentBlockContents().size() - i),
  284. context.insts().Get(inst_id));
  285. }
  286. return context.inst_block_stack().Pop();
  287. }
  288. auto DiscardGenericDecl(Context& context) -> void {
  289. context.generic_region_stack().Pop();
  290. }
  291. auto BuildGeneric(Context& context, SemIR::InstId decl_id) -> SemIR::GenericId {
  292. auto all_bindings =
  293. context.scope_stack().compile_time_bindings_stack().PeekAllValues();
  294. if (all_bindings.empty()) {
  295. CARBON_CHECK(context.generic_region_stack().PeekDependentInsts().empty(),
  296. "Have dependent instruction {0} in declaration {1} but no "
  297. "compile time bindings are in scope.",
  298. context.insts().Get(context.generic_region_stack()
  299. .PeekDependentInsts()
  300. .front()
  301. .inst_id),
  302. context.insts().Get(decl_id));
  303. context.generic_region_stack().Pop();
  304. return SemIR::GenericId::None;
  305. }
  306. // Build the new Generic object. Note that we intentionally do not hold a
  307. // persistent reference to it throughout this function, because the `generics`
  308. // collection can have items added to it by import resolution while we are
  309. // building this generic.
  310. auto bindings_id = context.inst_blocks().Add(all_bindings);
  311. SemIR::GenericId generic_id = context.generics().Add(
  312. SemIR::Generic{.decl_id = decl_id,
  313. .bindings_id = bindings_id,
  314. .self_specific_id = SemIR::SpecificId::None});
  315. // MakeSelfSpecificId could cause something to be imported, which would
  316. // invalidate the return value of `context.generics().Get(generic_id)`.
  317. auto self_specific_id = MakeSelfSpecificId(context, generic_id);
  318. context.generics().Get(generic_id).self_specific_id = self_specific_id;
  319. return generic_id;
  320. }
  321. auto FinishGenericDecl(Context& context, SemIRLoc loc,
  322. SemIR::GenericId generic_id) -> void {
  323. if (!generic_id.has_value()) {
  324. return;
  325. }
  326. auto decl_block_id = MakeGenericEvalBlock(
  327. context, generic_id, SemIR::GenericInstIndex::Region::Declaration);
  328. context.generic_region_stack().Pop();
  329. context.generics().Get(generic_id).decl_block_id = decl_block_id;
  330. ResolveSpecificDeclaration(context, loc,
  331. context.generics().GetSelfSpecific(generic_id));
  332. }
  333. auto BuildGenericDecl(Context& context, SemIR::InstId decl_id)
  334. -> SemIR::GenericId {
  335. SemIR::GenericId generic_id = BuildGeneric(context, decl_id);
  336. if (generic_id.has_value()) {
  337. FinishGenericDecl(context, decl_id, generic_id);
  338. }
  339. return generic_id;
  340. }
  341. auto FinishGenericRedecl(Context& context, SemIR::InstId /*decl_id*/,
  342. SemIR::GenericId /*generic_id*/) -> void {
  343. // TODO: Compare contents of this declaration with the existing one on the
  344. // generic.
  345. context.generic_region_stack().Pop();
  346. }
  347. auto FinishGenericDefinition(Context& context, SemIR::GenericId generic_id)
  348. -> void {
  349. if (!generic_id.has_value()) {
  350. // TODO: We can have symbolic constants in a context that had a non-generic
  351. // declaration, for example if there's a local generic let binding in a
  352. // function definition. Handle this case somehow -- perhaps by forming
  353. // substituted constant values now.
  354. context.generic_region_stack().Pop();
  355. return;
  356. }
  357. auto definition_block_id = MakeGenericEvalBlock(
  358. context, generic_id, SemIR::GenericInstIndex::Region::Definition);
  359. context.generics().Get(generic_id).definition_block_id = definition_block_id;
  360. context.generic_region_stack().Pop();
  361. }
  362. static auto ResolveSpecificDeclaration(Context& context, SemIRLoc loc,
  363. SemIR::SpecificId specific_id) -> void {
  364. // If this is the first time we've formed this specific, evaluate its decl
  365. // block to form information about the specific.
  366. if (!context.specifics().Get(specific_id).decl_block_id.has_value()) {
  367. // Set a placeholder value as the decl block ID so we won't attempt to
  368. // recursively resolve the same specific.
  369. context.specifics().Get(specific_id).decl_block_id =
  370. SemIR::InstBlockId::Empty;
  371. auto decl_block_id =
  372. TryEvalBlockForSpecific(context, loc, specific_id,
  373. SemIR::GenericInstIndex::Region::Declaration);
  374. // Note that TryEvalBlockForSpecific may reallocate the list of specifics,
  375. // so re-lookup the specific here.
  376. context.specifics().Get(specific_id).decl_block_id = decl_block_id;
  377. }
  378. }
  379. auto MakeSpecific(Context& context, SemIRLoc loc, SemIR::GenericId generic_id,
  380. SemIR::InstBlockId args_id) -> SemIR::SpecificId {
  381. auto specific_id = context.specifics().GetOrAdd(generic_id, args_id);
  382. ResolveSpecificDeclaration(context, loc, specific_id);
  383. return specific_id;
  384. }
  385. auto MakeSpecific(Context& context, SemIRLoc loc, SemIR::GenericId generic_id,
  386. llvm::ArrayRef<SemIR::InstId> args) -> SemIR::SpecificId {
  387. auto args_id = context.inst_blocks().AddCanonical(args);
  388. return MakeSpecific(context, loc, generic_id, args_id);
  389. }
  390. static auto MakeSelfSpecificId(Context& context, SemIR::GenericId generic_id)
  391. -> SemIR::SpecificId {
  392. if (!generic_id.has_value()) {
  393. return SemIR::SpecificId::None;
  394. }
  395. auto& generic = context.generics().Get(generic_id);
  396. auto args = context.inst_blocks().Get(generic.bindings_id);
  397. // Form a canonical argument list for the generic.
  398. llvm::SmallVector<SemIR::InstId> arg_ids;
  399. arg_ids.reserve(args.size());
  400. for (auto arg_id : args) {
  401. arg_ids.push_back(context.constant_values().GetConstantInstId(arg_id));
  402. }
  403. auto args_id = context.inst_blocks().AddCanonical(arg_ids);
  404. return context.specifics().GetOrAdd(generic_id, args_id);
  405. }
  406. auto MakeSelfSpecific(Context& context, SemIRLoc loc,
  407. SemIR::GenericId generic_id) -> SemIR::SpecificId {
  408. // Build a corresponding specific.
  409. SemIR::SpecificId specific_id = MakeSelfSpecificId(context, generic_id);
  410. // TODO: This could be made more efficient. We don't need to perform
  411. // substitution here; we know we want identity mappings for all constants and
  412. // types. We could also consider not storing the mapping at all in this case.
  413. ResolveSpecificDeclaration(context, loc, specific_id);
  414. return specific_id;
  415. }
  416. auto ResolveSpecificDefinition(Context& context, SemIRLoc loc,
  417. SemIR::SpecificId specific_id) -> bool {
  418. // TODO: Handle recursive resolution of the same generic definition.
  419. auto& specific = context.specifics().Get(specific_id);
  420. auto generic_id = specific.generic_id;
  421. CARBON_CHECK(generic_id.has_value(), "Specific with no generic ID");
  422. if (!specific.definition_block_id.has_value()) {
  423. // Evaluate the eval block for the definition of the generic.
  424. auto& generic = context.generics().Get(generic_id);
  425. if (!generic.definition_block_id.has_value()) {
  426. // The generic is not defined yet.
  427. return false;
  428. }
  429. auto definition_block_id = TryEvalBlockForSpecific(
  430. context, loc, specific_id, SemIR::GenericInstIndex::Region::Definition);
  431. // Note that TryEvalBlockForSpecific may reallocate the list of specifics,
  432. // so re-lookup the specific here.
  433. context.specifics().Get(specific_id).definition_block_id =
  434. definition_block_id;
  435. }
  436. return true;
  437. }
  438. auto GetInstForSpecific(Context& context, SemIR::SpecificId specific_id)
  439. -> SemIR::InstId {
  440. CARBON_CHECK(specific_id.has_value());
  441. const auto& specific = context.specifics().Get(specific_id);
  442. const auto& generic = context.generics().Get(specific.generic_id);
  443. auto decl = context.insts().Get(generic.decl_id);
  444. CARBON_KIND_SWITCH(decl) {
  445. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  446. return context.types().GetInstId(
  447. context.GetClassType(class_decl.class_id, specific_id));
  448. }
  449. case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
  450. return context.types().GetInstId(
  451. context.GetInterfaceType(interface_decl.interface_id, specific_id));
  452. }
  453. case SemIR::FunctionDecl::Kind: {
  454. return context.constant_values().GetInstId(
  455. TryEvalInst(context, SemIR::InstId::None,
  456. SemIR::SpecificFunction{
  457. .type_id = context.GetSingletonType(
  458. SemIR::SpecificFunctionType::SingletonInstId),
  459. .callee_id = generic.decl_id,
  460. .specific_id = specific_id}));
  461. }
  462. case SemIR::AssociatedConstantDecl::Kind: {
  463. // TODO: We don't have a good instruction to use here.
  464. return generic.decl_id;
  465. }
  466. default: {
  467. CARBON_FATAL("Unknown kind for generic declaration {0}", decl);
  468. }
  469. }
  470. }
  471. } // namespace Carbon::Check