mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Special-case api/impl implicit imports and verify relevant redeclarations. (#3843)
Adds ImportIRId::ApiForImpl to reserve a specific slot for the `api` import, so that the code can trivially determine whether an import is from the same library. This is then used for merging function declarations, because the rules for redeclarations in the same library slightly differ as compared to other imports (note they're also not identical to same-file rules). The main thing this leaves from the recent #3762 is verifying that entities forward declared in the `impl` file are also defined, but that's not in-scope for merging; it's moreso post-checking validation. Note, a lot of our `invalid <entity> ID` comments in ids.h were incorrectly copy-pasted, so I've cut `<entity>`.
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
// 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
|
||||
//
|
||||
// AUTOUPDATE
|
||||
|
||||
// --- basic.carbon
|
||||
|
||||
library "basic" api;
|
||||
|
||||
fn A();
|
||||
|
||||
// --- basic.impl.carbon
|
||||
|
||||
library "basic" impl;
|
||||
|
||||
fn A();
|
||||
|
||||
// --- extern_api.carbon
|
||||
|
||||
library "extern_api" api;
|
||||
|
||||
extern fn A();
|
||||
|
||||
// --- fail_extern_api.impl.carbon
|
||||
|
||||
library "extern_api" impl;
|
||||
|
||||
// CHECK:STDERR: fail_extern_api.impl.carbon:[[@LINE+10]]:1: ERROR: Redeclarations in the same library must match use of `extern`.
|
||||
// CHECK:STDERR: fn A();
|
||||
// CHECK:STDERR: ^~~~~~~
|
||||
// CHECK:STDERR: fail_extern_api.impl.carbon:[[@LINE-5]]:1: In import.
|
||||
// CHECK:STDERR: library "extern_api" impl;
|
||||
// CHECK:STDERR: ^~~~~~~
|
||||
// CHECK:STDERR: extern_api.carbon:4:1: Previously declared here.
|
||||
// CHECK:STDERR: extern fn A();
|
||||
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
||||
// CHECK:STDERR:
|
||||
fn A();
|
||||
|
||||
// --- extern_impl.carbon
|
||||
|
||||
library "extern_impl" api;
|
||||
|
||||
fn A();
|
||||
|
||||
// --- fail_extern_impl.impl.carbon
|
||||
|
||||
library "extern_impl" impl;
|
||||
|
||||
// CHECK:STDERR: fail_extern_impl.impl.carbon:[[@LINE+9]]:1: ERROR: Redeclarations in the same library must match use of `extern`.
|
||||
// CHECK:STDERR: extern fn A();
|
||||
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
||||
// CHECK:STDERR: fail_extern_impl.impl.carbon:[[@LINE-5]]:1: In import.
|
||||
// CHECK:STDERR: library "extern_impl" impl;
|
||||
// CHECK:STDERR: ^~~~~~~
|
||||
// CHECK:STDERR: extern_impl.carbon:4:1: Previously declared here.
|
||||
// CHECK:STDERR: fn A();
|
||||
// CHECK:STDERR: ^~~~~~~
|
||||
extern fn A();
|
||||
|
||||
// CHECK:STDOUT: --- basic.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace [template] {
|
||||
// CHECK:STDOUT: .A = %A
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %A: <function> = fn_decl @A [template] {}
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @A();
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: --- basic.impl.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace [template] {
|
||||
// CHECK:STDOUT: .A = %A
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %import_ref: <function> = import_ref ir1, inst+1, loaded [template = imports.%A]
|
||||
// CHECK:STDOUT: %A: <function> = fn_decl @A [template] {}
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @A();
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: --- extern_api.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace [template] {
|
||||
// CHECK:STDOUT: .A = %A
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %A: <function> = fn_decl @A [template] {}
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: extern fn @A();
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: --- fail_extern_api.impl.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace [template] {
|
||||
// CHECK:STDOUT: .A = %A
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %import_ref: <function> = import_ref ir1, inst+1, loaded [template = imports.%A]
|
||||
// CHECK:STDOUT: %A: <function> = fn_decl @A [template] {}
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @A();
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: --- extern_impl.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace [template] {
|
||||
// CHECK:STDOUT: .A = %A
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %A: <function> = fn_decl @A [template] {}
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @A();
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: --- fail_extern_impl.impl.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace [template] {
|
||||
// CHECK:STDOUT: .A = %A
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %import_ref: <function> = import_ref ir1, inst+1, loaded [template = imports.%A]
|
||||
// CHECK:STDOUT: %A: <function> = fn_decl @A [template] {}
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: extern fn @A();
|
||||
// CHECK:STDOUT:
|
||||
Reference in New Issue
Block a user