Skip to content

Commit a920895

Browse files
committed
coverage: Add GlobalFileId for stricter type-checking of file IDs
We already had a dedicated `LocalFileId` index type, but previously we used a raw `u32` for global file IDs, because index types were harder to pass through FFI.
1 parent f380d23 commit a920895

File tree

1 file changed

+17
-7
lines changed
  • compiler/rustc_codegen_llvm/src/coverageinfo

1 file changed

+17
-7
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ impl GlobalFileTable {
163163
Self { raw_file_table }
164164
}
165165

166-
fn global_file_id_for_file_name(&self, file_name: Symbol) -> u32 {
166+
fn global_file_id_for_file_name(&self, file_name: Symbol) -> GlobalFileId {
167167
let raw_id = self.raw_file_table.get_index_of(&file_name).unwrap_or_else(|| {
168168
bug!("file name not found in prepared global file table: {file_name}");
169169
});
170170
// The raw file table doesn't include an entry for the working dir
171171
// (which has ID 0), so add 1 to get the correct ID.
172-
(raw_id + 1) as u32
172+
GlobalFileId::from_usize(raw_id + 1)
173173
}
174174

175175
fn make_filenames_buffer(&self, tcx: TyCtxt<'_>) -> Vec<u8> {
@@ -198,27 +198,37 @@ impl GlobalFileTable {
198198
}
199199

200200
rustc_index::newtype_index! {
201+
/// An index into the CGU's overall list of file paths. The underlying paths
202+
/// will be embedded in the `__llvm_covmap` linker section.
203+
struct GlobalFileId {}
204+
}
205+
rustc_index::newtype_index! {
206+
/// An index into a function's list of global file IDs. That underlying list
207+
/// of local-to-global mappings will be embedded in the function's record in
208+
/// the `__llvm_covfun` linker section.
201209
struct LocalFileId {}
202210
}
203211

204212
/// Holds a mapping from "local" (per-function) file IDs to "global" (per-CGU)
205213
/// file IDs.
206214
#[derive(Default)]
207215
struct VirtualFileMapping {
208-
local_to_global: IndexVec<LocalFileId, u32>,
209-
global_to_local: FxIndexMap<u32, LocalFileId>,
216+
local_to_global: IndexVec<LocalFileId, GlobalFileId>,
217+
global_to_local: FxIndexMap<GlobalFileId, LocalFileId>,
210218
}
211219

212220
impl VirtualFileMapping {
213-
fn local_id_for_global(&mut self, global_file_id: u32) -> LocalFileId {
221+
fn local_id_for_global(&mut self, global_file_id: GlobalFileId) -> LocalFileId {
214222
*self
215223
.global_to_local
216224
.entry(global_file_id)
217225
.or_insert_with(|| self.local_to_global.push(global_file_id))
218226
}
219227

220228
fn into_vec(self) -> Vec<u32> {
221-
self.local_to_global.raw
229+
// This conversion should hopefully optimized away to ~zero overhead.
230+
// Even if it doesn't, it's probably not hot enough to worry about.
231+
self.local_to_global.into_iter().map(|global| global.as_u32()).collect()
222232
}
223233
}
224234

@@ -256,7 +266,7 @@ fn encode_mappings_for_function(
256266

257267
// Associate that global file ID with a local file ID for this function.
258268
let local_file_id = virtual_file_mapping.local_id_for_global(global_file_id);
259-
debug!(" file id: {local_file_id:?} => global {global_file_id} = '{file_name:?}'");
269+
debug!(" file id: {local_file_id:?} => {global_file_id:?} = '{file_name:?}'");
260270

261271
// For each counter/region pair in this function+file, convert it to a
262272
// form suitable for FFI.

0 commit comments

Comments
 (0)