requirements.references.selector_inline_and_foreign
Requirements index · View definition
Example
A function
runinside an inlinemod local { ... }insrc/lib.rshas selectormod[local]/fn[run]. A function defined in a separatesrc/local.rsfile has a selector within that file, without reconstructing the path of amod local;declaration elsewhere. A foreign functionputsin anextern "C"block has selectorextern[C]/fn[puts].
Rationale
The example is covered by three {test} references in src/outline/tests.rs. Together they cover all three parts of its scenario. I read the tests and their helpers. I did not run them.
- Inline module:
rust_selectors_follow_syntactic_ancestry(line 35) andselectors_name_syntactic_ancestry_and_duplicates(line 662) both parsemod local { fn run() {} }assrc/lib.rsorlib.rs. Both assert thatmod[local]/fn[run]is among the outline’s selectors. The first test also checks thatoutline.resolve(selector)returns a node whose selector equals the one asked for. - Foreign function: both tests parse
extern "C" { fn puts(); }and assertextern[C]/fn[puts]. The first also checks that anextern { fn other(); }block gets the default ABI name,extern[Rust]/fn[other]. - Separate module file:
functions_in_a_separate_module_file_are_selected_within_that_file(line 1059) parseslib.rscontainingmod local;and gets exactly["mod[local]"]. It parseslocal.rscontainingpub fn run() {}and gets exactly["fn[run]"]. It also asserts that no selector inlocal.rscontainsmod[local]. This shows the outline does not rebuild the module path from the declaration in another file, which is the “without reconstructing the path” part of the example.
The helpers outline and selectors call the real parse and collect the node selectors, so the assertions exercise the real outline code rather than a stub. The tests cover both the positive outcomes and the cross-file non-resolution that the example requires.
Inputs
Tests
Input 3: outline src/outline/tests.rs fn[rust_selectors_follow_syntactic_ancestry]
- File:
src/outline/tests.rs - Selector:
fn[rust_selectors_follow_syntactic_ancestry] - Kind:
fn src/outline/tests.rs:192–258
// {test}`requirements.references.outline_selectors`
// {test}`requirements.references.rust_item_selectors`
// {test}`requirements.references.selector_nested`
// {test}`requirements.references.selector_impl_and_trait`
// {test}`requirements.references.selector_inline_and_foreign`
// {test}`requirements.references.selector_duplicate_names`
// {test}`requirements.references.outlines`
#[test]
fn rust_selectors_follow_syntactic_ancestry() {
let source = r#"
mod local { fn run() {} }
mod external;
struct Thing;
impl Thing { fn run() {} }
impl Trait for Thing { fn run() {} }
impl [u8; 4] { fn edge() {} }
trait Trait { fn read(&self); type Item; }
extern "C" { fn puts(); }
extern { fn other(); }
fn outer() { fn inner() {} }
#[cfg(a)] fn repeated() {}
#[cfg(b)] fn repeated() {}
macro_rules! make { () => {} }
union U { a: u8 }
enum E { A }
const C: u8 = 1;
static S: u8 = 1;
type Alias = u8;
extern crate core;
"#;
let outline = outline("src/lib.rs", source);
let selectors = selectors(&outline);
for selector in [
"mod[local]/fn[run]",
"mod[external]",
"struct[Thing]",
"impl[Thing]/fn[run]",
"impl[Thing/Trait]/fn[run]",
"impl[%5Bu8;%204%5D]/fn[edge]",
"trait[Trait]/fn[read]",
"trait[Trait]/type[Item]",
"extern[C]/fn[puts]",
"extern[Rust]/fn[other]",
"fn[outer]/fn[inner]",
"fn[repeated]#1",
"fn[repeated]#2",
"macro[make]",
"union[U]",
"enum[E]",
"const[C]",
"static[S]",
"type[Alias]",
"extern-crate[core]",
] {
assert!(
selectors.contains(&selector.to_owned()),
"{selector}: {selectors:?}"
);
assert_eq!(
outline.nodes[outline.resolve(selector).unwrap()].selector,
selector
);
}
assert_eq!(outline.resolve("").unwrap(), 0);
assert!(outline.resolve("fn[repeated]").is_err());
assert!(outline.resolve("fn[missing]").is_err());
}
Input 4: outline src/outline/tests.rs fn[selectors_name_syntactic_ancestry_and_duplicates]
- File:
src/outline/tests.rs - Selector:
fn[selectors_name_syntactic_ancestry_and_duplicates] - Kind:
fn src/outline/tests.rs:840–885
// {test}`requirements.references.rust_item_selectors`
// {test}`requirements.references.selector_nested`
// {test}`requirements.references.selector_impl_and_trait`
// {test}`requirements.references.selector_inline_and_foreign`
// {test}`requirements.references.selector_duplicate_names`
// {test}`requirements.references.selector_use`
// {test}`requirements.references.outlines`
#[test]
fn selectors_name_syntactic_ancestry_and_duplicates() {
let source = r#"
use alpha::A;
use beta::B;
mod local { fn run() {} }
mod external;
struct Thing;
impl Thing { fn run() {} }
impl Trait for Thing { fn run() {} }
impl [u8; 4] { fn edge() {} }
trait Trait { fn read(&self); }
extern "C" { fn puts(); }
fn outer() { fn inner() {} }
#[cfg(a)] fn repeated() {}
#[cfg(b)] fn repeated() {}
"#;
let selectors = selectors(&outline("lib.rs", source));
assert!(selectors[0].starts_with("use@") && selectors[1].starts_with("use@"));
assert_ne!(selectors[0], selectors[1]);
for selector in [
"mod[local]/fn[run]",
"mod[external]",
"struct[Thing]",
"impl[Thing]/fn[run]",
"impl[Thing/Trait]/fn[run]",
"impl[%5Bu8;%204%5D]/fn[edge]",
"trait[Trait]/fn[read]",
"extern[C]/fn[puts]",
"fn[outer]/fn[inner]",
"fn[repeated]#1",
"fn[repeated]#2",
] {
assert!(
selectors.contains(&selector.to_owned()),
"{selector}: {selectors:?}"
);
}
}
Input 5: outline src/outline/tests.rs fn[functions_in_a_separate_module_file_are_selected_within_that_file]
- File:
src/outline/tests.rs - Selector:
fn[functions_in_a_separate_module_file_are_selected_within_that_file] - Kind:
fn src/outline/tests.rs:1163–1178
// {test}`requirements.references.selector_inline_and_foreign`
#[test]
fn functions_in_a_separate_module_file_are_selected_within_that_file() {
// `src/lib.rs` declares `mod local;`; `src/local.rs` holds its body. The
// function's selector is relative to `src/local.rs`, without the module
// path that the declaration in another file implies.
let declaring = selectors(&outline("lib.rs", "mod local;\n"));
assert_eq!(declaring, vec!["mod[local]".to_owned()]);
let module = selectors(&outline("local.rs", "pub fn run() {}\n"));
assert_eq!(module, vec!["fn[run]".to_owned()]);
assert!(
!module
.iter()
.any(|selector| selector.contains("mod[local]"))
);
}
Referenced definitions
Input 2: requirements selection requirements.references.rust_item_selectors
requirements.references.rust_item_selectors:md/requirements/references.md:344–354
:::{req} rust_item_selectors
The Rust outline profile under {cite}`requirements.references.outlines` MUST
identify items by segments describing their syntactic ancestry within the file,
such as `fn[run]`, `struct[Packet]`, and `mod[local]/fn[run]`, and MUST NOT
resolve Rust modules across files. Functions, methods, structs, enums, unions,
traits, impls, modules, macros, constants, statics, type aliases, and extern
blocks MUST be outline nodes. Impl segments MUST be named by their syntactic self
type and optional trait path, as `impl[Thing]` and `impl[Thing/Trait]`. `use`
declarations and other items without a suitable name MUST be anonymous. Selector
spelling MUST be documented.
:::
Other sources
Input 1: requirements selection requirements.references.selector_inline_and_foreign
requirements.references.selector_inline_and_foreign:md/requirements/references.md:449–451
:::{example} selector_inline_and_foreign
A function `run` inside an inline `mod local { ... }` in `src/lib.rs` has selector `mod[local]/fn[run]`. A function defined in a separate `src/local.rs` file has a selector within that file, without reconstructing the path of a `mod local;` declaration elsewhere. A foreign function `puts` in an `extern "C"` block has selector `extern[C]/fn[puts]`. {cite}`requirements.references.rust_item_selectors`
:::
Input 6: outline src/outline/tests.rs use@e843693d3cbe
- File:
src/outline/tests.rs - Selector:
use@e843693d3cbe - Kind:
use src/outline/tests.rs:1–1
use std::path::Path;
Input 7: outline src/outline/tests.rs use@bcaf6fa45a89
- File:
src/outline/tests.rs - Selector:
use@bcaf6fa45a89 - Kind:
use src/outline/tests.rs:3–3
use super::{Outline, parse};
Input 8: outline src/outline/tests.rs fn[outline]
- File:
src/outline/tests.rs - Selector:
fn[outline] - Kind:
fn src/outline/tests.rs:5–7
fn outline(path: &str, source: &str) -> Outline {
parse(Path::new(path), source).unwrap_or_else(|error| panic!("{path}: {error}"))
}
Input 9: outline src/outline/tests.rs fn[selectors]
- File:
src/outline/tests.rs - Selector:
fn[selectors] - Kind:
fn src/outline/tests.rs:9–14
fn selectors(outline: &Outline) -> Vec<String> {
outline.nodes[1..]
.iter()
.map(|node| node.selector.clone())
.collect()
}
Input 10: outline src/outline/tests.rs fn[summary]
- File:
src/outline/tests.rs - Selector:
fn[summary] - Kind:
fn src/outline/tests.rs:176–190
fn summary(outline: &Outline) -> Vec<String> {
outline.nodes[1..]
.iter()
.map(|node| {
format!(
"{} {}-{}",
regex::Regex::new("@[0-9a-f]{12}")
.unwrap()
.replace_all(&node.selector, "@"),
node.line_start,
node.line_end
)
})
.collect()
}