exint 0.1.4

An implementation of generic signed and unsigned integers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Test runner for LLVM FileCheck
//!
//! https://llvm.org/docs/LangRef.html
//! https://llvm.org/docs/CommandGuide/FileCheck.html

use rustc_driver::Callbacks;
use rustc_driver::catch_with_exit_code;
use rustc_driver::run_compiler;
use rustc_interface::Config;
use rustc_lint_defs::Level;
use rustc_session::config::CrateType;
use rustc_session::config::ExternEntry;
use rustc_session::config::ExternLocation;
use rustc_session::config::Externs;
use rustc_session::config::Input;
use rustc_session::config::OptLevel;
use rustc_session::config::OutputType;
use rustc_session::config::OutputTypes;
use rustc_session::search_paths::PathKind;
use rustc_session::search_paths::SearchPath;
use rustc_session::utils::CanonicalizedPath;
use rustc_span::edition::Edition;
use rustc_target::spec::MergeFunctions;
use rustc_target::spec::TargetTuple;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fs;
use std::io::Error;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::process::ExitCode;
use std::process::Output;
use std::process::Stdio;
use std::sync::LazyLock;

// -----------------------------------------------------------------------------
// Context
// -----------------------------------------------------------------------------

pub(crate) struct Context {
  pub(crate) llvm: Cow<'static, str>,
  pub(crate) root: Cow<'static, Path>,
}

impl Context {
  #[inline]
  pub(crate) fn new() -> Result<Self, Error> {
    let this: Self = Self {
      llvm: Cow::Borrowed("FileCheck"),
      root: workspace_root().map(Cow::Owned)?,
    };

    fs::create_dir_all(this.deps_path())?;

    Ok(this)
  }

  pub(crate) fn root_path(&self) -> &Path {
    self.root.as_ref()
  }

  pub(crate) fn test_path(&self) -> PathBuf {
    self.root.join("tests")
  }

  pub(crate) fn deps_path(&self) -> PathBuf {
    self.artifacts().join("deps")
  }

  pub(crate) fn artifacts(&self) -> PathBuf {
    self.test_path().join(".artifacts")
  }

  #[inline]
  pub(crate) fn llvm(mut self, value: impl Into<Cow<'static, str>>) -> Self {
    self.llvm = value.into();
    self
  }
}

// -----------------------------------------------------------------------------
// Capture
// -----------------------------------------------------------------------------

#[derive(Debug)]
pub(crate) struct Capture {
  pub(crate) kind: CrateType,
  pub(crate) name: &'static str,
  pub(crate) path: PathBuf,
}

impl Capture {
  pub(crate) const fn new_rlib(name: &'static str, path: PathBuf) -> Self {
    Self::with_type(name, path, CrateType::Rlib)
  }

  pub(crate) const fn new_proc_macro(name: &'static str, path: PathBuf) -> Self {
    Self::with_type(name, path, CrateType::ProcMacro)
  }

  pub(crate) const fn with_type(name: &'static str, path: PathBuf, kind: CrateType) -> Self {
    Self { name, path, kind }
  }

  pub(crate) fn check(self, context: &Context) -> Result<(), Error> {
    let output: Output = Command::new(context.llvm.as_ref())
      .arg("--color")
      .arg("--input-file")
      .arg(context.deps_path().join(format!("{}.ll", self.name)))
      .arg(self.path.as_path())
      .stdout(Stdio::piped())
      .stderr(Stdio::piped())
      .stdin(Stdio::null())
      .spawn()?
      .wait_with_output()?;

    let stdout: Cow<'_, str> = String::from_utf8_lossy(output.stdout.as_slice());
    let stderr: Cow<'_, str> = String::from_utf8_lossy(output.stderr.as_slice());

    fs::write(context.artifacts().join("out"), stdout.as_ref())?;
    fs::write(context.artifacts().join("err"), stderr.as_ref())?;

    if !output.status.success() {
      return Err(Error::other(stderr));
    }

    Ok(())
  }

  pub(crate) fn rustc(callbacks: &mut (dyn Callbacks + Send)) -> Result<(), Error> {
    let code: ExitCode = catch_with_exit_code(|| {
      run_compiler(&[const { String::new() }; 2], callbacks)
    });

    if code == ExitCode::FAILURE {
      Err(Error::other("RUSTC"))
    } else {
      Ok(())
    }
  }

  pub(crate) fn to_extern_entry(&self, deps: &Path) -> ExternEntry {
    let path: CanonicalizedPath = self.extern_path(deps);

    ExternEntry {
      location: ExternLocation::ExactPaths(BTreeSet::from([path])),
      is_private_dep: false,
      add_prelude: true,
      nounused_dep: false,
      force: false,
    }
  }

  fn extern_path(&self, deps: &Path) -> CanonicalizedPath {
    match self.kind {
      CrateType::Rlib => {
        CanonicalizedPath::new(deps.join(format!("lib{}.rlib", self.name)))
      }
      CrateType::Cdylib | CrateType::Dylib | CrateType::ProcMacro | CrateType::Sdylib => {
        CanonicalizedPath::new(deps.join(format!("lib{}.dylib", self.name)))
      }
      CrateType::Executable => {
        panic!("not supported: Executable")
      }
      CrateType::StaticLib => {
        panic!("not supported: StaticLib")
      }
    }
  }
}

// -----------------------------------------------------------------------------
// Callbacks
// -----------------------------------------------------------------------------

const PROC_MACRO_EXTERN: ExternEntry = ExternEntry {
  location: ExternLocation::FoundInLibrarySearchDirectories,
  is_private_dep: false,
  add_prelude: true,
  nounused_dep: false,
  force: false,
};

static LINT_OPTS: LazyLock<Vec<(String, Level)>> = LazyLock::new(|| {
  vec![
    ("incomplete_features".to_owned(), Level::Allow),
    ("internal_features".to_owned(), Level::Allow),
  ]
});

static LLVM_ARGS: LazyLock<Vec<String>> = LazyLock::new(|| {
  vec![
    "--aarch64-neon-syntax=apple".to_owned(),
    "--x86-asm-syntax=intel".to_owned(),
    "--unroll-threshold=9999".to_owned(),
  ]
});

// -----------------------------------------------------------------------------
// Callbacks (Default)
// -----------------------------------------------------------------------------

pub(crate) struct CallbacksDefault {
  externs: BTreeMap<String, ExternEntry>,
  features: BTreeSet<&'static str>,
  root_path: PathBuf,
  deps_path: PathBuf,
  crate_name: &'static str,
}

impl CallbacksDefault {
  const TARGET: &'static str = "aarch64-apple-darwin";

  pub(crate) fn new(name: &'static str, root: &Path, deps: &Path) -> Self {
    Self {
      externs: BTreeMap::new(),
      features: BTreeSet::new(),
      root_path: root.to_path_buf(),
      deps_path: deps.to_path_buf(),
      crate_name: name,
    }
  }

  pub(crate) fn features<I>(&mut self, name: I)
  where
    I: IntoIterator<Item = &'static str>,
  {
    self.features.extend(name);
  }

  pub(crate) fn dependency(&mut self, deps: &Path, krate: &Capture) {
    let _unused: Option<ExternEntry> = self
      .externs
      .insert(krate.name.to_owned(), krate.to_extern_entry(deps));
  }

  fn crate_cfg(&self) -> Vec<String> {
    fn __transform(name: &'static str) -> String {
      let mut buffer: String = String::with_capacity(10 + name.len());
      buffer.push_str("feature=");
      buffer.push('"');
      buffer.push_str(name);
      buffer.push('"');
      buffer
    }

    self.features.iter().copied().map(__transform).collect()
  }
}

impl Callbacks for CallbacksDefault {
  fn config(&mut self, config: &mut Config) {
    // -------------------------------------------------------------------------
    // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Config.html
    // -------------------------------------------------------------------------

    config.crate_cfg = self.crate_cfg();
    config.input = Input::File(self.root_path.clone());
    config.output_dir = Some(self.deps_path.clone());

    // -------------------------------------------------------------------------
    // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/options/struct.Options.html
    // -------------------------------------------------------------------------

    config.opts.crate_name = Some(self.crate_name.to_owned());
    config.opts.debug_assertions = false;
    config.opts.edition = Edition::Edition2024;
    config.opts.externs = Externs::new(self.externs.clone());
    config.opts.lint_opts = LINT_OPTS.clone();
    config.opts.optimize = OptLevel::Aggressive;
    config.opts.search_paths = [SearchPath::new(PathKind::Dependency, self.deps_path.clone())].to_vec();
    config.opts.target_triple = TargetTuple::TargetTuple(Self::TARGET.to_owned());
    config.opts.verbose = true;

    // -------------------------------------------------------------------------
    // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/config/struct.CodegenOptions.html
    // -------------------------------------------------------------------------

    config.opts.cg.debug_assertions = Some(config.opts.debug_assertions);
    config.opts.cg.llvm_args = LLVM_ARGS.clone();
    config.opts.cg.opt_level = "3".to_owned();

    // -------------------------------------------------------------------------
    // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/config/struct.UnstableOptions.html
    // -------------------------------------------------------------------------

    // Ensure consistent order of compiled output
    config.opts.unstable_opts.codegen_source_order = true;

    // Disable function merging - messes with LLVM FileCheck
    config.opts.unstable_opts.merge_functions = Some(MergeFunctions::Disabled);
  }
}

// -----------------------------------------------------------------------------
// Callbacks (RLib)
// -----------------------------------------------------------------------------

pub(crate) struct CallbacksRLib {
  default: CallbacksDefault,
}

impl CallbacksRLib {
  pub(crate) const fn new(default: CallbacksDefault) -> Self {
    Self { default }
  }
}

impl Callbacks for CallbacksRLib {
  fn config(&mut self, config: &mut Config) {
    self.default.config(config);

    config.opts.crate_types = [CrateType::Rlib].to_vec();

    config.opts.output_types = OutputTypes::new(&[
      (OutputType::DepInfo, None),
      (OutputType::Exe, None),
      (OutputType::Metadata, None),
    ]);
  }
}

// -----------------------------------------------------------------------------
// Callbacks (Assembly)
// -----------------------------------------------------------------------------

pub(crate) struct CallbacksAssembly {
  default: CallbacksDefault,
}

impl CallbacksAssembly {
  pub(crate) const fn new(default: CallbacksDefault) -> Self {
    Self { default }
  }
}

impl Callbacks for CallbacksAssembly {
  fn config(&mut self, config: &mut Config) {
    self.default.config(config);

    config.opts.crate_types = [CrateType::Rlib].to_vec();

    config.opts.output_types = OutputTypes::new(&[
      (OutputType::Assembly, None),
      (OutputType::LlvmAssembly, None),
    ]);
  }
}

// -----------------------------------------------------------------------------
// Callbacks (Proc Macro)
// -----------------------------------------------------------------------------

pub(crate) struct CallbacksProcMacro {
  default: CallbacksDefault,
}

impl CallbacksProcMacro {
  pub(crate) const fn new(default: CallbacksDefault) -> Self {
    Self { default }
  }
}

impl Callbacks for CallbacksProcMacro {
  fn config(&mut self, config: &mut Config) {
    let _unused: Option<ExternEntry> = self
      .default
      .externs
      .insert("proc_macro".to_owned(), PROC_MACRO_EXTERN);

    self.default.config(config);

    config.opts.crate_types = [CrateType::ProcMacro].to_vec();

    config.opts.output_types = OutputTypes::new(&[
      (OutputType::DepInfo, None),
      (OutputType::Exe, None),
    ]);
  }
}

// -----------------------------------------------------------------------------
// Misc. Utilities
// -----------------------------------------------------------------------------

fn locate_project() -> Result<Output, Error> {
  let output: Output = Command::new("cargo")
    .arg("locate-project")
    .arg("--workspace")
    .arg("--message-format")
    .arg("plain")
    .output()?;

  if output.status.success() {
    return Ok(output);
  }

  let content: &[u8] = output.stderr.as_slice();
  let message: Cow<'_, str> = String::from_utf8_lossy(content);

  Err(Error::other(message))
}

fn workspace_root() -> Result<PathBuf, Error> {
  let output: Output = locate_project()?;
  let stdout: &[u8] = output.stdout.as_slice();

  str::from_utf8(stdout)
    .ok()
    .map(str::trim)
    .map(Path::new)
    .and_then(Path::parent)
    .map(Path::to_path_buf)
    .ok_or_else(|| Error::other("failed to read path from `cargo locate-project`"))
}