mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Performing a lookup against `Self` inside the definition of the named constraint leads to cycles, as described in the document [Self contradictions in Named Constraints](https://docs.google.com/document/d/17rn2XmME8o2MM4OJqatSVuMa1iYZ1PAgcNrf0PXR9Q4/edit?tab=t.0). To prevent those cycles, this change introduces a large refactoring of impl lookup. The impl lookup done inside eval is reduced to only performing monomorphization. That is it: - Only looks for an provides final witnesses. - Is not allowed to identify the facet type of the query self. - Returns either a final witness or None (or an error) The paths for finding non-final witnesses are now done outside of eval, directly in the initial `LookupImplWitness()` function. If no final witness it found through eval, the resulting non-final `LookupImplWitness` instruction witness is returned. It does not produce cycles to identify the facet type of query self outside of eval, since that does not result in repeating the identification when resolving specifics of the named constraint or require decl. Move the ArrayStack for Context::require_impls_stack into a new class which tracks a NamedConstraintId (or InterfaceId) for each frame of RequireImplsIds, so that in type completion we always can find the correct frame for a given named constraint which is still being defined, in order to find the RequireImplsIds in the in-progress definition.
37 lines
1.1 KiB
C++
37 lines
1.1 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/check/require_impls_stack.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto RequireImplsStack::Push(EnclosingScopeId scope_id) -> void {
|
|
scope_ids_.push_back(scope_id);
|
|
array_stack_.PushArray();
|
|
}
|
|
|
|
auto RequireImplsStack::Pop() -> void {
|
|
scope_ids_.pop_back();
|
|
array_stack_.PopArray();
|
|
}
|
|
|
|
auto RequireImplsStack::AppendToTop(SemIR::RequireImplsId id) -> void {
|
|
array_stack_.AppendToTop(id);
|
|
}
|
|
|
|
auto RequireImplsStack::PeekTop() const
|
|
-> llvm::ArrayRef<SemIR::RequireImplsId> {
|
|
return array_stack_.PeekArray();
|
|
}
|
|
|
|
auto RequireImplsStack::PeekForScope(EnclosingScopeId scope_id)
|
|
-> llvm::ArrayRef<SemIR::RequireImplsId> {
|
|
auto it = std::find(scope_ids_.rbegin(), scope_ids_.rend(), scope_id);
|
|
CARBON_CHECK(it != scope_ids_.rend());
|
|
auto index = std::distance(it, scope_ids_.rend()) - 1;
|
|
return array_stack_.PeekArrayAt(index);
|
|
}
|
|
|
|
} // namespace Carbon::Check
|