requirements.references.selector_nested
Requirements index · View definition
Example
In
src/lib.rs,fn outer() { fn inner() {} }gives the selectorsfn[outer]andfn[outer]/fn[inner].
Rationale
The example requirements.references.selector_nested is a concrete case. In src/lib.rs, the source fn outer() { fn inner() {} } is expected to give the selectors fn[outer] and fn[outer]/fn[inner].
Two tests carry {test} references to it:
src/outline/tests.rs#fn[rust_selectors_follow_syntactic_ancestry]uses the pathsrc/lib.rsand a source containing exactlyfn outer() { fn inner() {} }. It asserts that the outline’s selector list containsfn[outer]/fn[inner]. It also asserts thatoutline.resolve("fn[outer]/fn[inner]")returns a node whoseselectorequals that string.src/outline/tests.rs#fn[selectors_name_syntactic_ancestry_and_duplicates]makes the same containment assertion on a similar source.
Neither test asserts fn[outer] on its own. I checked src/outline/mod.rs, and the resolve assertion covers it anyway:
Outline::resolvewalks the segments one at a time and takes a child of the current node for each. To resolvefn[outer]/fn[inner], it must first find a root child whose segment isfn[outer].disambiguatebuilds each node’s selector asparent.selector + "/" + segment, and a node whose parent is the root gets just its segment. So the node forfn[outer]has the selectorfn[outer].
The two assertions together check both selectors in the example, including the nested spelling. A wrong ancestry form would fail them. Examples would be a flat fn[inner], a different separator, or a missing outer node. I found no unresolved gap, and there are no acknowledgments to weigh.
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:?}"
);
}
}
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_nested
requirements.references.selector_nested:md/requirements/references.md:441–443
:::{example} selector_nested
In `src/lib.rs`, `fn outer() { fn inner() {} }` gives the selectors `fn[outer]` and `fn[outer]/fn[inner]`. {cite}`requirements.references.rust_item_selectors`
:::
Input 5: 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 6: outline src/outline/mod.rs fn[disambiguate]
- File:
src/outline/mod.rs - Selector:
fn[disambiguate] - Kind:
fn src/outline/mod.rs:628–655
/// Adds `#N` to sibling segments that would otherwise be identical, except
/// `line` nodes, and builds full selectors.
fn disambiguate(nodes: &mut [OutlineNode]) {
for parent in 0..nodes.len() {
let mut groups = BTreeMap::<String, Vec<usize>>::new();
for child in &nodes[parent].children {
groups
.entry(nodes[*child].segment.clone())
.or_default()
.push(*child);
}
for group in groups.values().filter(|group| group.len() > 1) {
for (ordinal, child) in group.iter().enumerate() {
if nodes[*child].kind != "line" {
nodes[*child].segment = format!("{}#{}", nodes[*child].segment, ordinal + 1);
}
}
}
}
for index in 1..nodes.len() {
let parent = nodes[index].parent.unwrap_or(0);
nodes[index].selector = if parent == 0 {
nodes[index].segment.clone()
} else {
format!("{}/{}", nodes[parent].selector, nodes[index].segment)
};
}
}
Input 7: outline src/outline/mod.rs impl[Outline]/fn[resolve]
- File:
src/outline/mod.rs - Selector:
impl[Outline]/fn[resolve] - Kind:
fn src/outline/mod.rs:805–820
/// Resolves a selector to a node index; the empty selector is the root.
/// A `line` segment resolves to the first line with that content.
pub(crate) fn resolve(&self, selector: &str) -> Result<usize, ResolveError> {
let segments = parse_selector(selector).map_err(ResolveError::Invalid)?;
let mut current = 0;
for segment in segments {
let wanted = segment.to_string();
current = self.nodes[current]
.children
.iter()
.copied()
.find(|child| self.nodes[*child].segment == wanted)
.ok_or_else(|| ResolveError::NotFound(selector.to_owned()))?;
}
Ok(current)
}