Start including the prelude for testing. (#3861)

- Adds an empty prelude.carbon file
- Imports that file in any non-Core package file
  - Adds --disable-prelude-import to avoid that
- Adds --exclude-dump-file-prefix to be able to hide files from dumping
- Used to hide core files (we can't do this by package name due to lex
dumps, for example)
- Restructures some tests to not rely on `i32`, particularly `alias`
tests (which rely on a name ref) and tests with no prelude.

I'm adding the framework for switching i32 to calling Int32 in the
prelude, but I'm running into a separate error actually switching over.
So that *mostly* works, but isn't quite ready for prime time. However,
maybe the current state of this PR is still useful to review since it
does a lot of the infrastructure work and adds the %Core everywhere?
This commit is contained in:
Jon Ross-Perkins
2024-04-07 17:11:42 +00:00
committed by GitHub
parent f1dc7d0093
commit 895e90e791
497 changed files with 2902 additions and 1210 deletions
+5 -53
View File
@@ -12,65 +12,15 @@
namespace Carbon::Check {
// Returns the scope of the Core package, or Invalid if it's not found.
//
// TODO: Consider tracking the Core package in SemIR so we don't need to use
// name lookup to find it.
static auto GetCorePackage(Context& context, Parse::AnyExprId node_id)
-> SemIR::NameScopeId {
// TODO: If the current package is the `Core` package, return
// `SemIR::InstId::Package`.
auto ident_id = context.identifiers().Lookup("Core");
if (!ident_id.is_valid()) {
return SemIR::NameScopeId::Invalid;
}
auto name_id = SemIR::NameId::ForIdentifier(ident_id);
// Look up `package.Core`.
auto package_id = context.LookupQualifiedName(
node_id, name_id, SemIR::NameScopeId::Package, /*required=*/false);
if (!package_id.is_valid()) {
return SemIR::NameScopeId::Invalid;
}
// Look through import_refs and aliases.
package_id = context.constant_values().Get(package_id).inst_id();
// We expect it to be a package, and fail if not.
if (auto package_inst =
context.insts().TryGetAs<SemIR::Namespace>(package_id)) {
auto& name_scope = context.name_scopes().Get(package_inst->name_scope_id);
// Check that this is really the `Core` package and not an alias.
if (name_scope.is_closed_import && name_scope.name_id == name_id &&
name_scope.enclosing_scope_id == SemIR::NameScopeId::Package) {
return package_inst->name_scope_id;
}
}
return SemIR::NameScopeId::Invalid;
}
// Returns the name scope of the operator interface for the specified operator
// from the Core package.
static auto GetOperatorInterface(Context& context, Parse::AnyExprId node_id,
Operator op) -> SemIR::NameScopeId {
auto carbon_package_id = GetCorePackage(context, node_id);
if (!carbon_package_id.is_valid()) {
auto interface_id = context.LookupNameInCore(node_id, op.interface_name);
if (interface_id == SemIR::InstId::BuiltinError) {
return SemIR::NameScopeId::Invalid;
}
// Lookup `Core.InterfaceName`.
auto interface_ident_id = context.identifiers().Add(op.interface_name);
auto interface_id = context.LookupQualifiedName(
node_id, SemIR::NameId::ForIdentifier(interface_ident_id),
carbon_package_id, /*required=*/false);
if (!interface_id.is_valid()) {
return SemIR::NameScopeId::Invalid;
}
// Look through import_refs and aliases.
interface_id = context.constant_values().Get(interface_id).inst_id();
// We expect it to be an interface.
if (auto interface_inst =
context.insts().TryGetAs<SemIR::InterfaceType>(interface_id)) {
@@ -110,7 +60,9 @@ auto BuildUnaryOperator(Context& context, Parse::AnyExprId node_id, Operator op,
SemIR::InstId operand_id) -> SemIR::InstId {
auto op_fn = GetOperatorOpFunction(context, node_id, op);
if (!op_fn.is_valid()) {
context.TODO(node_id, "missing or invalid operator interface");
context.TODO(node_id,
"missing or invalid operator interface, also avoid duplicate "
"diagnostic if prelude is unavailable");
return SemIR::InstId::BuiltinError;
}