Files
carbon-lang/toolchain/check/impl_validation.h
T
Dana Jansensandjosh11b 950d83451a Add diagnostics for invalid impl declarations (#5420)
Outside of `match_first` this adds diagnostics for invalid non-final and
final `impl` declarations in line with those being proposed in
https://github.com/carbon-language/carbon-lang/pull/5337.

- Two non-final `impl`s with the exact same type structure is invalid.
- A `final impl` that matches the self/constraint of another `impl` as a
query would always be preferred, making the second one invalid.
- Two `final impl`s that overlap (have compatible type structures) in
different files is invalid.
- Two `final impl`s that overlap (have compatible type structures) in
the same file is invalid outside of `match_first`.
- A `final impl` in a different file from its root self type and
interface is invalid.

We add tests for all these scenarios as well as correct scenarios.

The "compatible" test for two type structures was being done
symmetrically, which is incorrect. We want it to test that a query type
structure is the same _or more specific_ in a compatible way with an
impl's type structure. This is corrected in the implementation, and the
diagnostics now have to test both directions to get the desired output,
as expected.

---------

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
2025-05-22 18:58:47 +00:00

20 lines
684 B
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_IMPL_VALIDATION_H_
#define CARBON_TOOLCHAIN_CHECK_IMPL_VALIDATION_H_
#include "toolchain/check/context.h"
namespace Carbon::Check {
// Called after typechecking the full file to diagnose any `impl` declarations
// that are invalid because they are in wrong file or overlap with other `impl`
// declarations incorrectly.
auto ValidateImplsInFile(Context& context) -> void;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_IMPL_VALIDATION_H_