handle_where.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/context.h"
  5. #include "toolchain/check/convert.h"
  6. #include "toolchain/check/facet_type.h"
  7. #include "toolchain/check/generic.h"
  8. #include "toolchain/check/handle.h"
  9. #include "toolchain/check/inst.h"
  10. #include "toolchain/check/type.h"
  11. #include "toolchain/sem_ir/facet_type_info.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. auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool {
  17. // The expression at the top of the stack represents a constraint type that
  18. // is being modified by the `where` operator. It would be `MyInterface` in
  19. // `MyInterface where .Member = i32`.
  20. auto [self_node, self_id] = context.node_stack().PopExprWithNodeId();
  21. auto self_with_constraints_type_id =
  22. ExprAsType(context, self_node, self_id).type_id;
  23. // Only facet types may have `where` restrictions.
  24. if (self_with_constraints_type_id != SemIR::ErrorInst::TypeId &&
  25. !context.types().IsFacetType(self_with_constraints_type_id)) {
  26. CARBON_DIAGNOSTIC(WhereOnNonFacetType, Error,
  27. "left argument of `where` operator must be a facet type");
  28. context.emitter().Emit(self_node, WhereOnNonFacetType);
  29. self_with_constraints_type_id = SemIR::ErrorInst::TypeId;
  30. }
  31. // Strip off any constraints provided by a `WhereExpr` from the `Self` facet
  32. // type. For a facet type like `I & J where .X = .Y`, this will reduce it down
  33. // to just `I & J`.
  34. //
  35. // Any references to `.Self` in constraints for the current `WhereExpr` will
  36. // not see constraints in the `Self` facet type, but they will resolve to
  37. // values through the constraints explicitly when they are combined together.
  38. auto self_without_constraints_type_id = self_with_constraints_type_id;
  39. if (auto facet_type = context.types().TryGetAs<SemIR::FacetType>(
  40. self_without_constraints_type_id)) {
  41. const auto& info = context.facet_types().Get(facet_type->facet_type_id);
  42. auto stripped_info =
  43. SemIR::FacetTypeInfo{.extend_constraints = info.extend_constraints};
  44. stripped_info.Canonicalize();
  45. self_without_constraints_type_id = GetFacetType(context, stripped_info);
  46. }
  47. // Introduce a name scope so that we can remove the `.Self` entry we are
  48. // adding to name lookup at the end of the `where` expression.
  49. context.scope_stack().PushForSameRegion();
  50. // Introduce `.Self` as a symbolic binding. Its type is the value of the
  51. // expression to the left of `where`, so `MyInterface` in the example above.
  52. auto period_self_inst_id =
  53. MakePeriodSelfFacetValue(context, self_without_constraints_type_id);
  54. // Save the `.Self` symbolic binding on the node stack. It will become the
  55. // first argument to the `WhereExpr` instruction.
  56. context.node_stack().Push(node_id, period_self_inst_id);
  57. // Going to put each requirement on `args_type_info_stack`, so we can have an
  58. // inst block with the varying number of requirements but keeping other
  59. // instructions on the current inst block from the `inst_block_stack()`.
  60. context.args_type_info_stack().Push();
  61. // Pass along all the constraints from the base facet type to be added to the
  62. // resulting facet type.
  63. context.args_type_info_stack().AddInstId(
  64. AddInstInNoBlock<SemIR::RequirementBaseFacetType>(
  65. context, SemIR::LocId(node_id),
  66. {.base_type_inst_id =
  67. context.types().GetInstId(self_with_constraints_type_id)}));
  68. // Add a context stack for tracking rewrite constraints, that will be used to
  69. // allow later constraints to read from them eagerly.
  70. context.rewrites_stack().emplace_back();
  71. // Make rewrite constraints from the self facet type available immediately to
  72. // expressions in rewrite constraints for this `where` expression.
  73. if (auto self_facet_type = context.types().TryGetAs<SemIR::FacetType>(
  74. self_with_constraints_type_id)) {
  75. const auto& base_facet_type_info =
  76. context.facet_types().Get(self_facet_type->facet_type_id);
  77. for (const auto& rewrite : base_facet_type_info.rewrite_constraints) {
  78. if (rewrite.lhs_id != SemIR::ErrorInst::InstId) {
  79. context.rewrites_stack().back().Insert(
  80. context.constant_values().Get(
  81. GetImplWitnessAccessWithoutSubstitution(context,
  82. rewrite.lhs_id)),
  83. rewrite.rhs_id);
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89. auto HandleParseNode(Context& context, Parse::RequirementEqualId node_id)
  90. -> bool {
  91. auto [rhs_node, rhs_id] = context.node_stack().PopExprWithNodeId();
  92. auto lhs_id = context.node_stack().PopExpr();
  93. // Convert rhs to type of lhs.
  94. auto lhs_type_id = context.insts().Get(lhs_id).type_id();
  95. if (lhs_type_id.is_symbolic()) {
  96. // If the type of the associated constant is symbolic, we defer conversion
  97. // until the constraint is resolved, in case it depends on `Self` (which
  98. // will now be a reference to `.Self`).
  99. // For now we convert to a value expression eagerly because otherwise we'll
  100. // often be unable to constant-evaluate the enclosing `where` expression.
  101. // TODO: Perform the conversion symbolically and add an implicit constraint
  102. // that this conversion is valid and produces a constant.
  103. rhs_id = ConvertToValueExpr(context, rhs_id);
  104. } else {
  105. rhs_id = ConvertToValueOfType(context, rhs_node, rhs_id,
  106. context.insts().Get(lhs_id).type_id());
  107. }
  108. // Build up the list of arguments for the `WhereExpr` inst.
  109. context.args_type_info_stack().AddInstId(
  110. AddInstInNoBlock<SemIR::RequirementRewrite>(
  111. context, node_id, {.lhs_id = lhs_id, .rhs_id = rhs_id}));
  112. if (lhs_id != SemIR::ErrorInst::InstId) {
  113. // Track the value of the rewrite so further constraints can use it
  114. // immediately, before they are evaluated. This happens directly where the
  115. // `ImplWitnessAccess` that refers to the rewrite constraint would have been
  116. // created, and the value of the constraint will be used instead.
  117. context.rewrites_stack().back().Insert(
  118. context.constant_values().Get(
  119. GetImplWitnessAccessWithoutSubstitution(context, lhs_id)),
  120. rhs_id);
  121. }
  122. return true;
  123. }
  124. auto HandleParseNode(Context& context, Parse::RequirementEqualEqualId node_id)
  125. -> bool {
  126. auto rhs = context.node_stack().PopExpr();
  127. auto lhs = context.node_stack().PopExpr();
  128. // TODO: Type check lhs and rhs are comparable.
  129. // TODO: Require that at least one side uses a designator.
  130. // Build up the list of arguments for the `WhereExpr` inst.
  131. context.args_type_info_stack().AddInstId(
  132. AddInstInNoBlock<SemIR::RequirementEquivalent>(
  133. context, node_id, {.lhs_id = lhs, .rhs_id = rhs}));
  134. return true;
  135. }
  136. auto HandleParseNode(Context& context, Parse::RequirementImplsId node_id)
  137. -> bool {
  138. auto [rhs_node, rhs_id] = context.node_stack().PopExprWithNodeId();
  139. auto [lhs_node, lhs_id] = context.node_stack().PopExprWithNodeId();
  140. // Check lhs is a facet and rhs is a facet type.
  141. auto lhs_as_type = ExprAsType(context, lhs_node, lhs_id);
  142. auto rhs_as_type = ExprAsType(context, rhs_node, rhs_id);
  143. if (rhs_as_type.type_id != SemIR::ErrorInst::TypeId &&
  144. !context.types().IsFacetType(rhs_as_type.type_id)) {
  145. CARBON_DIAGNOSTIC(
  146. ImplsOnNonFacetType, Error,
  147. "right argument of `impls` requirement must be a facet type");
  148. context.emitter().Emit(rhs_node, ImplsOnNonFacetType);
  149. rhs_as_type.inst_id = SemIR::ErrorInst::TypeInstId;
  150. }
  151. // TODO: Require that at least one side uses a designator.
  152. // TODO: For things like `HashSet(.T) as type`, add an implied constraint
  153. // that `.T impls Hash`.
  154. // Build up the list of arguments for the `WhereExpr` inst.
  155. context.args_type_info_stack().AddInstId(
  156. AddInstInNoBlock<SemIR::RequirementImpls>(
  157. context, node_id,
  158. {.lhs_id = lhs_as_type.inst_id, .rhs_id = rhs_as_type.inst_id}));
  159. return true;
  160. }
  161. auto HandleParseNode(Context& /*context*/, Parse::RequirementAndId /*node_id*/)
  162. -> bool {
  163. // Nothing to do.
  164. return true;
  165. }
  166. auto HandleParseNode(Context& context, Parse::WhereExprId node_id) -> bool {
  167. context.rewrites_stack().pop_back();
  168. // Remove `PeriodSelf` from name lookup, undoing the `Push` done for the
  169. // `WhereOperand`.
  170. context.scope_stack().Pop();
  171. SemIR::InstId period_self_id =
  172. context.node_stack().Pop<Parse::NodeKind::WhereOperand>();
  173. SemIR::InstBlockId requirements_id = context.args_type_info_stack().Pop();
  174. AddInstAndPush<SemIR::WhereExpr>(context, node_id,
  175. {.type_id = SemIR::TypeType::TypeId,
  176. .period_self_id = period_self_id,
  177. .requirements_id = requirements_id});
  178. return true;
  179. }
  180. } // namespace Carbon::Check