crates/mimir-driver/tests/fixture/a/src/lib.rs
//! fixture crate a 0.1.0
pub trait Greet {
fn greet(&self) -> String;
}
#[derive(Debug, Clone, Copy)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl Point {
pub fn new(x: i32, y: i32) -> Self {
Point { x, y }
}
pub fn norm1(&self) -> i32 {
self.x.abs() + self.y.abs()
}
}
macro_rules! twice {
($e:expr) => {
$e + $e
};
}
// never inlined: the binary layer's fixture needs `add` as a linked symbol
// (a small leaf fn is otherwise auto-inlined across crates in --release).
#[inline(never)]
pub fn add(x: i32, y: i32) -> i32 {
twice!(x) - x + y
}
unsafe extern "C" {
/// libc `abs` — the extern "C" border.
pub fn abs(v: i32) -> i32;
/// libc `getpid` — an import LLVM cannot fold away (it folds `abs`), so
/// the linked binary keeps it as an undefined symbol.
pub fn getpid() -> i32;
}
/// generic + `#[inline(never)]`: b instantiates it twice (`u32`, `&str`) →
/// two monomorphised instances in the linked binary.
#[inline(never)]
pub fn ident<T: core::fmt::Debug>(t: T) -> String {
format!("{t:?}")
}
/// always inlined: a source node with no linked symbol of its own.
#[inline(always)]
pub fn tiny(x: i32) -> i32 {
x * 3
}
/// called by nothing: the linker drops it from b's binaries.
pub fn dead(x: i32) -> i32 {
x.rotate_left(7) ^ 0x5a
}
// ---- visibility (schema 2) ------------------------------------------------
// at the crate root `pub(crate)` and no qualifier are the same set of places
pub(crate) fn crate_root_scoped() -> i32 {
private_fn()
}
fn private_fn() -> i32 {
inner::for_parent() + inner::crate_wide()
}
mod inner {
/// Re-exported from a private module.
pub fn reexported() -> i32 {
deep::scoped() + deep::up()
}
// `pub`, yet nothing outside the crate can reach it
pub fn hidden_pub() -> i32 {
2
}
pub(super) fn for_parent() -> i32 {
3
}
pub(crate) fn crate_wide() -> i32 {
4
}
/// Not nameable from outside, but `leak()` hands it out.
pub struct Leaked;
pub mod deep {
pub(in crate::inner) fn scoped() -> i32 {
5
}
pub(super) fn up() -> i32 {
6
}
}
}
pub use inner::reexported;
pub fn leak() -> inner::Leaked {
let _ = (crate_root_scoped(), inner::hidden_pub());
inner::Leaked
}
// ---- features and gates ---------------------------------------------------
/// Compiled in: `on` is a default feature.
#[cfg(feature = "on")]
pub fn gated_on() -> i32 {
1
}
/// Compiled OUT: nothing enables `extra`.
#[cfg(feature = "extra")]
pub fn extra() {}
#[cfg(not(feature = "extra"))]
pub fn not_extra() {}
#[cfg(all(feature = "on", not(feature = "extra")))]
pub fn on_not_extra() {}
// the usual pair: one of the two exists in any build
#[cfg(feature = "extra")]
pub fn pick() -> i32 {
1
}
#[cfg(not(feature = "extra"))]
pub fn pick() -> i32 {
2
}
// a whole module file rustc never loads
#[cfg(feature = "extra")]
mod extra_mod;
// a feature no manifest declares (rustc warns `unexpected_cfgs`: that is the point)
#[cfg(feature = "ghost")]
fn ghost() {}
pub struct Config {
pub always: i32,
#[cfg(feature = "extra")]
pub extra_field: i32,
#[cfg(feature = "on")]
pub on_field: i32,
}
impl Config {
#[cfg(feature = "extra")]
pub fn extra_method(&self) -> i32 {
self.extra_field
}
}
// ---- gates INSIDE an item: the item is there either way, a part of it is not
// a gated `use` is no node: it is a part of the module it stands in
#[cfg(feature = "extra")]
use core::fmt as _gated_use;
pub fn body_gate() -> i32 {
if cfg!(feature = "extra") { 1 } else { 0 }
}
pub fn stmt_gates() -> i32 {
let mut v = 0;
#[cfg(feature = "on")]
{
v += 1;
}
#[cfg(not(feature = "extra"))]
{
v += 2;
}
#[cfg(feature = "extra")]
{
v += 4;
}
v
}
// its own gate AND a gate inside it, same feature: one edge, not partial
#[cfg(feature = "on")]
pub fn own_and_inside() -> i32 {
#[cfg(feature = "on")]
let v = 1;
v
}
// a gate inside an item that is itself compiled out
#[cfg(feature = "extra")]
pub fn extra_with_inside() -> i32 {
#[cfg(feature = "on")]
let v = 2;
v
}
// switches an ATTRIBUTE, not code: counted, never an edge
#[cfg_attr(feature = "extra", allow(dead_code))]
pub fn attr_only() {}
#[cfg(test)]
mod tests {
#[test]
fn adds() {
assert_eq!(super::add(1, 2), 3);
}
// ---- attributes (schema 3): `#[ignore]`, bare and with a reason
#[test]
#[ignore]
fn slow() {}
#[test]
#[ignore = "needs the network"]
fn slow_with_reason() {}
}
// ---- attributes (schema 3): `#[doc(hidden)]` -------------------------------
/// Public, yet not part of the documented API.
#[doc(hidden)]
pub fn hidden_api() -> i32 {
7
}
#[doc(hidden)]
pub mod hidden_mod {
// inside a hidden module: not itself marked
pub fn inside() -> i32 {
8
}
}