|
5 | 5 | #include "toolchain/check/facet_type.h" |
6 | 6 |
|
7 | 7 | #include "toolchain/check/convert.h" |
| 8 | +#include "toolchain/check/generic.h" |
8 | 9 | #include "toolchain/check/import_ref.h" |
9 | 10 | #include "toolchain/check/inst.h" |
10 | 11 | #include "toolchain/check/interface.h" |
11 | 12 | #include "toolchain/check/type.h" |
12 | 13 | #include "toolchain/check/type_completion.h" |
| 14 | +#include "toolchain/sem_ir/ids.h" |
13 | 15 | #include "toolchain/sem_ir/typed_insts.h" |
14 | 16 |
|
15 | 17 | namespace Carbon::Check { |
@@ -164,24 +166,10 @@ auto InitialFacetTypeImplWitness( |
164 | 166 | continue; |
165 | 167 | } |
166 | 168 |
|
167 | | - if (table_entry != SemIR::ImplWitnessTablePlaceholder::TypeInstId) { |
168 | | - if (table_entry != rewrite_inst_id) { |
169 | | - // TODO: Figure out how to print the two different values |
170 | | - // `const_id` & `rewrite_inst_id` in the diagnostic |
171 | | - // message. |
172 | | - CARBON_DIAGNOSTIC( |
173 | | - AssociatedConstantWithDifferentValues, Error, |
174 | | - "associated constant {0} given two different values {1} and {2}", |
175 | | - SemIR::NameId, InstIdAsConstant, InstIdAsConstant); |
176 | | - auto& assoc_const = context.associated_constants().Get( |
177 | | - assoc_constant_decl->assoc_const_id); |
178 | | - context.emitter().Emit( |
179 | | - facet_type_inst_id, AssociatedConstantWithDifferentValues, |
180 | | - assoc_const.name_id, table_entry, rewrite_inst_id); |
181 | | - } |
182 | | - table_entry = SemIR::ErrorInst::InstId; |
183 | | - continue; |
184 | | - } |
| 169 | + // FacetTypes resolution disallows two rewrites to the same associated |
| 170 | + // constant, so we won't ever have a facet write twice to the same position |
| 171 | + // in the witness table. |
| 172 | + CARBON_CHECK(table_entry == SemIR::ImplWitnessTablePlaceholder::TypeInstId); |
185 | 173 |
|
186 | 174 | // If the associated constant has a symbolic type, convert the rewrite |
187 | 175 | // value to that type now we know the value of `Self`. |
@@ -263,4 +251,103 @@ auto AllocateFacetTypeImplWitness(Context& context, |
263 | 251 | context.inst_blocks().ReplacePlaceholder(witness_id, empty_table); |
264 | 252 | } |
265 | 253 |
|
| 254 | +auto IsPeriodSelf(Context& context, SemIR::ConstantId const_id) -> bool { |
| 255 | + // This also rejects the singleton Error value as it's concrete. |
| 256 | + if (!const_id.is_symbolic()) { |
| 257 | + return false; |
| 258 | + } |
| 259 | + const auto& symbolic = |
| 260 | + context.constant_values().GetSymbolicConstant(const_id); |
| 261 | + // Fast early reject before doing more expensive operations. |
| 262 | + if (symbolic.dependence != SemIR::ConstantDependence::PeriodSelf) { |
| 263 | + return false; |
| 264 | + } |
| 265 | + return IsPeriodSelf(context, symbolic.inst_id); |
| 266 | +} |
| 267 | + |
| 268 | +auto IsPeriodSelf(Context& context, SemIR::InstId inst_id) -> bool { |
| 269 | + // Unwrap the `FacetAccessType` instruction, which we get when the `.Self` is |
| 270 | + // converted to `type`. |
| 271 | + if (auto facet_access_type = |
| 272 | + context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) { |
| 273 | + inst_id = facet_access_type->facet_value_inst_id; |
| 274 | + } |
| 275 | + if (auto bind_symbolic_name = |
| 276 | + context.insts().TryGetAs<SemIR::BindSymbolicName>(inst_id)) { |
| 277 | + const auto& bind_name = |
| 278 | + context.entity_names().Get(bind_symbolic_name->entity_name_id); |
| 279 | + return bind_name.name_id == SemIR::NameId::PeriodSelf; |
| 280 | + } |
| 281 | + return false; |
| 282 | +} |
| 283 | + |
| 284 | +auto ResolveRewriteConstraintsAndCanonicalize(Context& context, |
| 285 | + SemIR::LocId loc_id, |
| 286 | + SemIR::FacetTypeInfo& facet_type) |
| 287 | + -> void { |
| 288 | + // This operation sorts and dedupes the rewrite constraints. They are sorted |
| 289 | + // primarily by the `lhs_id`, then by the `rhs_id`. |
| 290 | + facet_type.Canonicalize(); |
| 291 | + |
| 292 | + if (facet_type.rewrite_constraints.empty()) { |
| 293 | + return; |
| 294 | + } |
| 295 | + |
| 296 | + for (size_t i = 0; i < facet_type.rewrite_constraints.size() - 1; ++i) { |
| 297 | + auto& constraint = facet_type.rewrite_constraints[i]; |
| 298 | + if (constraint.lhs_id == SemIR::ErrorInst::InstId || |
| 299 | + constraint.rhs_id == SemIR::ErrorInst::InstId) { |
| 300 | + continue; |
| 301 | + } |
| 302 | + |
| 303 | + auto lhs_access = |
| 304 | + context.insts().TryGetAs<SemIR::ImplWitnessAccess>(constraint.lhs_id); |
| 305 | + if (!lhs_access) { |
| 306 | + continue; |
| 307 | + } |
| 308 | + auto lhs_lookup = context.insts().TryGetAs<SemIR::LookupImplWitness>( |
| 309 | + lhs_access->witness_id); |
| 310 | + if (!lhs_lookup) { |
| 311 | + continue; |
| 312 | + } |
| 313 | + if (!IsPeriodSelf(context, lhs_lookup->query_self_inst_id)) { |
| 314 | + continue; |
| 315 | + } |
| 316 | + |
| 317 | + // This loop moves `i` to the last position with the same LHS value, so that |
| 318 | + // we don't diagnose more than once within the same contiguous range of |
| 319 | + // assignments to a single LHS value. |
| 320 | + for (; i < facet_type.rewrite_constraints.size() - 1; ++i) { |
| 321 | + auto& next = facet_type.rewrite_constraints[i + 1]; |
| 322 | + if (constraint.lhs_id != next.lhs_id) { |
| 323 | + break; |
| 324 | + } |
| 325 | + // `constraint.lhs_id == next.lhs_id` so only check for `ErrorInst` in the |
| 326 | + // RHS. On the first error, `constraint.rhs_id` is set to `ErrorInst` |
| 327 | + // which prevents further diagnostics for the same LHS value due to this |
| 328 | + // condition. |
| 329 | + if (constraint.rhs_id != SemIR::ErrorInst::InstId && |
| 330 | + next.rhs_id != SemIR::ErrorInst::InstId) { |
| 331 | + CARBON_DIAGNOSTIC( |
| 332 | + AssociatedConstantWithDifferentValues, Error, |
| 333 | + "associated constant {0} given two different values {1} and {2}", |
| 334 | + InstIdAsConstant, InstIdAsConstant, InstIdAsConstant); |
| 335 | + // TODO: It would be nice to note the places where the values are |
| 336 | + // assigned but rewrite constraint instructions are from canonical |
| 337 | + // constant values, and have no locations. We'd need to store a location |
| 338 | + // along with them in the rewrite constraints. |
| 339 | + context.emitter().Emit(loc_id, AssociatedConstantWithDifferentValues, |
| 340 | + constraint.lhs_id, constraint.rhs_id, |
| 341 | + next.rhs_id); |
| 342 | + } |
| 343 | + constraint.rhs_id = SemIR::ErrorInst::InstId; |
| 344 | + next.rhs_id = SemIR::ErrorInst::InstId; |
| 345 | + } |
| 346 | + } |
| 347 | + |
| 348 | + // Canonicalize again, as we may have inserted errors into the rewrite |
| 349 | + // constraints, and these could change sorting order. |
| 350 | + facet_type.Canonicalize(); |
| 351 | +} |
| 352 | + |
266 | 353 | } // namespace Carbon::Check |
0 commit comments