handle_where.cpp 8.8 KB

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