Files
Dana Jansens d6be20641c Use earlier require decls inside a named constraint to provide witnesses for Self (#6915)
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.
2026-03-26 15:26:56 +00:00

45 lines
1.5 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
#ifndef CARBON_TOOLCHAIN_CHECK_REQUIRE_IMPLS_STACK_H_
#define CARBON_TOOLCHAIN_CHECK_REQUIRE_IMPLS_STACK_H_
#include "common/array_stack.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::Check {
// A stack where each frame holds an array of RequireImplsIds and is associated
// with an enclosing scope that can be searched for in the stack.
class RequireImplsStack {
public:
using EnclosingScopeId =
std::variant<SemIR::InterfaceId, SemIR::NamedConstraintId>;
// Push a new stack frame for an interface or named constraint to add
// RequireImplsIds.
auto Push(EnclosingScopeId scope_id) -> void;
// Pop the top stack frame and all its RequireImplsIds.
auto Pop() -> void;
// Append to the top stack frame.
auto AppendToTop(SemIR::RequireImplsId id) -> void;
// Returns the RequireImplsIds in the top stack frame.
auto PeekTop() const -> llvm::ArrayRef<SemIR::RequireImplsId>;
// Finds the stack frame for a given scope and returns the RequireImplsIds in
// that stack frame.
auto PeekForScope(EnclosingScopeId scope_id)
-> llvm::ArrayRef<SemIR::RequireImplsId>;
private:
llvm::SmallVector<EnclosingScopeId> scope_ids_;
ArrayStack<SemIR::RequireImplsId> array_stack_;
};
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_REQUIRE_IMPLS_STACK_H_