@exodus/stasis 1.0.0-beta.15 → 1.0.0-beta.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +1 -1
  2. package/bin/stasis.js +26 -11
  3. package/package.json +10 -9
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +113 -233
  6. package/src/babel.js +39 -122
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +5 -9
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +50 -166
  11. package/src/cmd/bundle-options.js +110 -167
  12. package/src/cmd/bundle.js +861 -1344
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +46 -27
  15. package/src/cmd/output.js +17 -12
  16. package/src/cmd/sbom.js +3 -8
  17. package/src/cmd/tokens.js +10 -23
  18. package/src/define.js +34 -97
  19. package/src/diff.js +72 -128
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +10 -25
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +41 -93
  27. package/src/loaders/cargo.js +298 -805
  28. package/src/loaders/foundry.js +109 -285
  29. package/src/loaders/paths.js +13 -13
  30. package/src/loaders/php.js +210 -489
  31. package/src/loaders/rust.js +507 -1430
  32. package/src/loaders/solidity-ownership.js +48 -128
  33. package/src/loaders/solidity.js +39 -122
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +9 -33
  37. package/src/metro-resolver.js +20 -47
  38. package/src/metro-transformer.js +0 -5
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +2 -5
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +90 -220
  44. package/src/resolve-node.js +27 -45
  45. package/src/resolve-typescript.js +109 -313
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +52 -128
  49. package/src/scan.js +136 -283
  50. package/src/sixel.js +18 -41
  51. package/src/table.js +14 -35
  52. package/src/validate-notes.d.ts +3 -2
  53. package/src/validate-notes.js +25 -19
  54. package/src/validate.d.ts +38 -11
  55. package/src/validate.js +478 -512
  56. package/src/vfs-bundle/entries.js +40 -57
  57. package/src/vfs-bundle/github.js +88 -121
  58. package/src/vfs-bundle/glob.js +4 -45
  59. package/src/vfs-bundle/lazy.js +27 -56
  60. package/src/vfs-bundle/prisma.js +167 -110
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +295 -318
  63. package/src/vfs-bundle/yarnclean.js +10 -31
  64. package/src/vfs-bundle.js +23 -64
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +75 -231
  67. package/src/installed-commits.js +0 -184
@@ -1,57 +1,29 @@
1
- // Rust loader: `{ sources, resolutions, missing }` from a crate's files.
2
- //
3
- // Reachability follows what rustc follows. A `mod foo;` declaration (incl. `#[path = …]` and
4
- // `#[cfg_attr(…, path = …)]`) pulls a module file in, and a reference to a crate whose source is
5
- // in-tree -- the package's own lib target, a Cargo `path` dependency, or a `cargo vendor`ed crate
6
- // -- pulls that crate's root in. `crate::` / `self::` / `super::` / relative paths resolve against
7
- // the per-crate module tree for the import graph but never widen the walk: a `use` can't add a
8
- // file to a crate, only `mod` can. Registry dependencies that aren't vendored live outside the
9
- // bundle root and are dropped.
1
+ // Rust loader. Reachability follows rustc: a `mod` (`#[path]`, `#[cfg_attr(…, path)]` too) pulls a module file in, a
2
+ // reference to an in-tree crate (own lib, path dep, vendored) its root; a `use` resolves against the module tree only.
10
3
 
11
4
  import { isUtf8 } from 'node:buffer'
12
5
  import { realpathSync } from 'node:fs'
13
6
  import { readFile } from 'node:fs/promises'
14
7
  import { join, posix, relative } from 'node:path'
15
8
 
16
- import { assertRealPathWithinBase, nestedMap, relativeEscapes, toPosix } from '@exodus/stasis-core/util'
9
+ import { assertRealPathWithinBase, memoized, nestedMap, relativeEscapes, toPosix, upsert } from '@exodus/stasis-core/util'
17
10
  import { isFile } from '../resolve-typescript.js'
18
- import { TARGET_CFG_KEYS, TARGET_UNIT, VENDOR_DIR, cached, cfgName, createCargoContext, evalCfg, evalCfgKey, isTestTargetPath, normName, normalizeCfg, normalizeRel } from './cargo.js'
11
+ import { TARGET_CFG_KEYS, TARGET_UNIT, VENDOR_DIR, cfgName, createCargoContext, evalCfg, evalCfgKey, isTestTargetPath, normName, normalizeCfg, normalizeRel } from './cargo.js'
19
12
  import { matchClose, splitTopLevel } from './toml.js'
20
13
 
21
- // Leads of the expression-position paths anchored on the module tree rather than on a name.
22
14
  const PATH_KEYWORDS = new Set(['crate', 'self', 'super'])
23
-
24
- // Path keywords, and the sysroot crates every build links: a path whose lead is one of these
25
- // never names an in-tree crate.
26
15
  const NON_CRATE_LEADS = new Set([...PATH_KEYWORDS, 'std', 'core', 'alloc'])
27
-
28
- // The other sysroot crates a program may name (`extern crate proc_macro;`, `test::Bencher`):
29
- // never in-tree, so never worth reporting as unresolved.
30
16
  const OTHER_SYSROOT_CRATES = new Set(['proc_macro', 'test'])
31
-
32
- // Files rustc treats as crate roots by name; every entry is one by role (see crateRoots).
33
17
  const ROOT_NAMES = new Set(['main.rs', 'lib.rs'])
34
-
35
- // Cap on `mod`-chain depth when building a module tree: with the `seen` set it bounds cycles
36
- // and the O(depth²) growth of joined module-path strings on absurd input.
18
+ // With the `seen` set, bounds cycles and the O(depth²) growth of module-path strings on absurd input.
37
19
  const MAX_MODULE_DEPTH = 1000
38
20
 
39
- const baseName = (p) => (p.includes('/') ? p.slice(p.lastIndexOf('/') + 1) : p)
40
- const isNamedRoot = (p) => ROOT_NAMES.has(baseName(p))
41
21
  const isWordChar = (ch) => ch !== undefined && /\w/u.test(ch)
42
22
 
43
- // --- Lexing -----------------------------------------------------------------------------
44
-
45
- // Single left-to-right pass over Rust source producing two same-length views (newlines kept, so
46
- // lines and offsets stay aligned with the input): `code` blanks comments only; `masked` blanks
47
- // comments AND the contents of string/char literals. Block comments nest; a `//` inside a string
48
- // is not a comment and a `/*` inside a `//` comment opens nothing -- rules a pair of regexes gets
49
- // wrong in both directions (a commented-out `mod` taken for a real one, or real ones swallowed).
23
+ // Two views aligned offset for offset with `content`: `code` blanks comments, `masked` string and char literal
24
+ // contents too. Block comments nest and a `//` in a string is no comment, which a pair of regexes gets wrong.
50
25
  export function lexRust(content) {
51
26
  const n = content.length
52
- // Each view is assembled from chunks: the source up to a blanked range, then the range with
53
- // every line replaced by as many spaces as it has UTF-16 units (a char may be two), so indexes
54
- // stay those of `content`.
55
27
  const code = []
56
28
  const masked = []
57
29
  let codeAt = 0
@@ -81,27 +53,21 @@ export function lexRust(content) {
81
53
  const start = i
82
54
  let depth = 0
83
55
  do {
84
- if (content[i] === '/' && content[i + 1] === '*') {
85
- depth++
86
- i += 2
87
- } else if (content[i] === '*' && content[i + 1] === '/') {
88
- depth--
89
- i += 2
90
- } else {
91
- i++
92
- }
56
+ const delta = content.startsWith('/*', i) ? 1 : (content.startsWith('*/', i) ? -1 : 0)
57
+ depth += delta
58
+ i += delta === 0 ? 1 : 2
93
59
  } while (i < n && depth > 0)
94
- blankBoth(start, Math.min(i, n))
60
+ blankBoth(start, i)
95
61
  continue
96
62
  }
97
63
  const lit = stringStart(content, i)
98
64
  if (lit) {
99
- const start = lit.open + 1 // past the `b`/`c`/`r#` prefix and the opening quote
65
+ const start = lit.open + 1
100
66
  if (lit.raw === null) {
101
67
  i = start
102
- while (i < n && content[i] !== '"') i += content[i] === '\\' ? 2 : 1 // an escape takes two
68
+ while (i < n && content[i] !== '"') i += content[i] === '\\' ? 2 : 1
103
69
  blankMasked(start, Math.min(i, n))
104
- i++ // closing quote
70
+ i++
105
71
  } else {
106
72
  const close = `"${'#'.repeat(lit.raw)}`
107
73
  let end = content.indexOf(close, start)
@@ -126,10 +92,7 @@ export function lexRust(content) {
126
92
  return { code: code.join(''), masked: masked.join('') }
127
93
  }
128
94
 
129
- // If a string literal starts at `i` -- counting a `b`/`c` byte/C-string prefix and the raw
130
- // marker `r`/`r#…#` -- return `{ open, raw }` where `open` indexes the opening quote and `raw` is
131
- // a raw string's `#` count (null for a plain one); else null. A prefix must not be the tail of
132
- // an identifier (`bar"` is not a prefix).
95
+ // A `b`/`c`/`r#…#` prefix must not be the tail of an identifier (`bar"`).
133
96
  function stringStart(content, i) {
134
97
  if (content[i] === '"') return { open: i, raw: null }
135
98
  let j = i
@@ -142,8 +105,7 @@ function stringStart(content, i) {
142
105
  return content[k] === '"' ? { open: k, raw: k - j - 1 } : null
143
106
  }
144
107
 
145
- // Index of the quote closing a char literal opening at `i` (`'x'`, `'\n'`, `'\u{1F600}'`), or -1
146
- // when the quote starts a lifetime/label (`'a`), which has no closing quote.
108
+ // The quote closing a char literal at `i`, or -1 for a lifetime or label (`'a`).
147
109
  function charLiteralEnd(content, i) {
148
110
  if (content[i + 1] === '\\') {
149
111
  let j = i + 2
@@ -151,50 +113,29 @@ function charLiteralEnd(content, i) {
151
113
  const close = content.indexOf('}', j)
152
114
  if (close === -1 || close - j > 10) return -1
153
115
  j = close + 1
154
- } else {
155
- j += 1
156
- }
116
+ } else j += 1
157
117
  return content[j] === "'" ? j : -1
158
118
  }
159
119
  if (content[i + 1] === undefined || content[i + 1] === "'") return -1
160
120
  if (content[i + 2] === "'") return i + 2
161
- // An astral char is two UTF-16 units.
162
- const cp = content.codePointAt(i + 1)
163
- return cp > 0xff_ff && content[i + 3] === "'" ? i + 3 : -1
121
+ return content.codePointAt(i + 1) > 0xff_ff && content[i + 3] === "'" ? i + 3 : -1 // an astral char is two UTF-16 units
164
122
  }
165
123
 
166
- // --- Item scanning ----------------------------------------------------------------------
167
-
168
124
  const WORD_RE = /[A-Za-z_]\w*/uy
169
125
  const WS_RE = /\s/u
170
- // First word of an item or statement that runs to its `;` or `{ … }` body, whatever commas its
171
- // generics, arguments or `where` clause hold; anything else (a field, a variant, a match arm)
172
- // ends at a top-level comma. Weak keywords a field may be named after (`default`, `union`) are
173
- // left out on purpose.
126
+ // Items run to their `;` or body whatever commas they hold; weak keywords a field may be named (`union`) are left out.
174
127
  const ITEM_KEYWORDS = new Set(['fn', 'impl', 'struct', 'enum', 'trait', 'type', 'const', 'static', 'mod', 'use', 'extern', 'macro_rules', 'unsafe', 'async', 'let'])
175
- // Macros whose literal string argument names a file relative to the invoking one: Rust source
176
- // spliced in (`include!`), or an asset embedded as text / bytes. Only a literal can be followed;
177
- // `concat!(env!("OUT_DIR"), …)` is build output.
178
128
  const INCLUDE_MACROS = new Set(['include', 'include_str', 'include_bytes'])
179
- // Macros whose body is a template of code for some other crate (a proc macro's output): nothing
180
- // in it is an item, import or path of the crate holding it.
129
+ // Macros whose body is code for some other crate (a proc macro's output): nothing in it is this crate's.
181
130
  const TEMPLATE_MACROS = new Set(['quote', 'quote_spanned', 'parse_quote', 'parse_quote_spanned'])
182
- // An include macro's argument: a string literal (plain, with escapes, or raw with any number of
183
- // `#`s), or `concat!(env!("CARGO_MANIFEST_DIR"), "<literal>")`, a path from the package root.
184
131
  const STRING_LITERAL_RE = /^\s*(?:"((?:[^"\\]|\\[\s\S])*)"|r(#*)"([\s\S]*?)"\2)\s*$/u
185
132
  const MANIFEST_DIR_CONCAT_RE = /^\s*concat!\s*\(\s*env!\s*\(\s*"CARGO_MANIFEST_DIR"\s*\)\s*,\s*([\s\S]*?),?\s*\)\s*$/u
186
- // An include macro opening inside an attribute's text: `#![doc = include_str!("../README.md")]`.
187
133
  const INCLUDE_IN_ATTR_RE = /\b(include|include_str|include_bytes)!\s*(?=[([{])/gu
188
- // Items a module defines by name, for `pub struct X` behind a glob; `mod`, `use`, `extern crate`
189
- // and `macro_rules!` have their own records.
190
134
  const DEFINING_KEYWORDS = new Set(['struct', 'enum', 'union', 'trait', 'type', 'fn', 'const', 'static'])
191
- // Of those, the items in the value namespace: never a path's lead or prefix (a `fn log` beside
192
- // `use log::info` hides no crate).
135
+ // Value-namespace items never lead a path: a `fn log` beside `use log::info` hides no crate.
193
136
  const VALUE_KEYWORDS = new Set(['fn', 'const', 'static'])
194
137
 
195
- // The path an include macro's argument names: `{ path, base }` (`base`: `file` for a path relative
196
- // to the invoking file, `manifest` for one from the package root), or null when it isn't one the
197
- // loader can follow (`concat!(env!("OUT_DIR"), …)` is build output).
138
+ // Null for an argument the loader can't follow (`concat!(env!("OUT_DIR"), …)` is build output).
198
139
  function includeArg(text) {
199
140
  const manifest = MANIFEST_DIR_CONCAT_RE.exec(text)
200
141
  const lit = STRING_LITERAL_RE.exec(manifest ? manifest[1] : text)
@@ -203,8 +144,6 @@ function includeArg(text) {
203
144
  return manifest ? { path: path.replace(/^\/+/u, ''), base: 'manifest' } : { path, base: 'file' }
204
145
  }
205
146
 
206
- // The value of a plain string literal's body: `\\`, `\"`, `\'`, `\n`, `\r`, `\t`, `\0`,
207
- // `\x41`, `\u{1F600}` and a backslash-newline continuation.
208
147
  function unescapeString(body) {
209
148
  return body.replaceAll(/\\(?:u\{([0-9a-fA-F]{1,6})\}|x([0-9a-fA-F]{2})|\n\s*|(.))/gsu, (m, u, x, c) => {
210
149
  if (u !== undefined) return String.fromCodePoint(Number.parseInt(u, 16))
@@ -215,68 +154,42 @@ function unescapeString(body) {
215
154
  }
216
155
  const EXTERN_CRATE_RE = /extern\s+crate\s+(?:r#)?(\w+)(?:\s+as\s+(?:r#)?(\w+))?\s*;/uy
217
156
  const MACRO_EXPORT_RE = /^\s*macro_export\s*(?:\([\s\S]*\))?\s*$/u
218
- // Expression-position paths: any `lead::…` path whose lead is lowercase (skipping `Type::assoc`
219
- // associated-item paths) -- a `crate`/`self`/`super` keyword path, or one led by a module or
220
- // crate name. Runs over the masked view, so string contents can't fake one.
157
+ // Run over the masked view, so string contents can't fake a path; `Type::assoc` is skipped.
221
158
  const LEAD_PATH_RE = /\b([a-z_]\w*)(?:::\w+)+/gu
222
159
  const ATTR_PATH_RE = /^\s*path\s*=\s*"([^"]*)"\s*$/u
160
+ const CFG_ATTR_RE = /^\s*cfg\s*\(([\s\S]*)\)\s*$/u
223
161
 
224
- // Combine an item's cfg predicates (several `#[cfg]` attributes all apply) into one; null when ungated.
225
162
  const joinCfgs = (preds) => (preds.length === 0 ? null : (preds.length === 1 ? preds[0] : `all(${preds.join(', ')})`))
226
163
 
227
- const CFG_ATTR_RE = /^\s*cfg\s*\(([\s\S]*)\)\s*$/u
228
-
229
- // One outer attribute's text (inside `#[…]`) → `cfg`: the predicate gating the item (`#[cfg(<pred>)]`;
230
- // `test` for a `#[test]` fn; for `#[cfg_attr(<pred>, cfg(<inner>))]` the item is compiled unless
231
- // pred holds and inner doesn't, i.e. `any(not(pred), inner)`; null when the item is unconditional
232
- // -- a `cfg_attr` applying any other attribute, `doc(cfg(…))` included, gates nothing);
233
- // `paths`: the module file paths it names, `#[path = "…"]` outright (cfg null) or each
234
- // `#[cfg_attr(<pred>, path = "…")]` under its predicate; `pred`: a `cfg_attr`'s own predicate
235
- // (null for any other attribute) -- when it can't hold, the whole attribute applies nothing;
236
- // `macroExport`: true for `#[macro_export]` / `#[macro_export(local_inner_macros)]`, outright or
237
- // under a `cfg_attr` (the macro then lives at the crate root, as `$crate::name!`); `includes`: the
238
- // files an include macro in the text names (`#[doc = include_str!("../README.md")]`, or from the
239
- // package root with `concat!(env!("CARGO_MANIFEST_DIR"), …)`; see includeArg). `masked` is the same
240
- // text with string contents blanked (lexRust), for matching the macro's brackets.
241
- function parseAttr(text, masked = text) {
164
+ // `cfg_attr(<pred>, cfg(<inner>))` gates the item on `any(not(pred), inner)`.
165
+ function parseAttr(text, masked) {
242
166
  const includes = []
243
167
  for (const m of masked.matchAll(INCLUDE_IN_ATTR_RE)) {
244
168
  const open = m.index + m[0].length
245
169
  const arg = includeArg(text.slice(open + 1, matchClose(masked, open)))
246
170
  if (arg !== null) includes.push({ kind: m[1], ...arg })
247
171
  }
248
- if (MACRO_EXPORT_RE.test(text)) return { cfg: null, paths: [], pred: null, includes, macroExport: true }
249
- if (/^\s*macro_use\s*$/u.test(text)) return { cfg: null, paths: [], pred: null, includes, macroUse: true }
172
+ const attr = (fields) => ({ cfg: null, paths: [], pred: null, includes, ...fields })
173
+ if (MACRO_EXPORT_RE.test(text)) return attr({ macroExport: true })
174
+ if (/^\s*macro_use\s*$/u.test(text)) return attr({ macroUse: true })
250
175
  const path = ATTR_PATH_RE.exec(text)
251
- if (path) return { cfg: null, paths: [{ path: path[1], cfg: null }], pred: null, includes }
252
- if (/^\s*test\s*$/u.test(text)) return { cfg: 'test', paths: [], pred: null, includes }
176
+ if (path) return attr({ paths: [{ path: path[1], cfg: null }] })
177
+ if (/^\s*test\s*$/u.test(text)) return attr({ cfg: 'test' })
253
178
  const cfg = CFG_ATTR_RE.exec(text)
254
- if (cfg) return { cfg: normalizeCfg(cfg[1]), paths: [], pred: null, includes }
179
+ if (cfg) return attr({ cfg: normalizeCfg(cfg[1]) })
255
180
  const cfgAttr = /^\s*cfg_attr\s*\(([\s\S]*)\)\s*$/u.exec(text)
256
- if (cfgAttr) {
257
- const parts = splitTopLevel(cfgAttr[1])
258
- const pred = normalizeCfg(parts.shift() ?? '')
259
- const paths = []
260
- const inner = []
261
- let macroExport = false
262
- for (const part of parts) {
263
- const m = ATTR_PATH_RE.exec(part)
264
- const c = CFG_ATTR_RE.exec(part)
265
- if (m) paths.push({ path: m[1], cfg: pred })
266
- else if (c) inner.push(normalizeCfg(c[1]))
267
- else if (MACRO_EXPORT_RE.test(part)) macroExport = true
268
- }
269
- return { cfg: inner.length === 0 ? null : `any(not(${pred}), ${joinCfgs(inner)})`, paths, pred, includes, macroExport }
270
- }
271
- return { cfg: null, paths: [], pred: null, includes }
181
+ if (!cfgAttr) return attr({})
182
+ const parts = splitTopLevel(cfgAttr[1])
183
+ const pred = normalizeCfg(parts.shift() ?? '')
184
+ const paths = parts.map((part) => ATTR_PATH_RE.exec(part)).filter(Boolean).map((m) => ({ path: m[1], cfg: pred }))
185
+ const inner = parts.map((part) => CFG_ATTR_RE.exec(part)).filter(Boolean).map((m) => normalizeCfg(m[1]))
186
+ const macroExport = parts.some((part) => MACRO_EXPORT_RE.test(part))
187
+ return attr({ cfg: inner.length === 0 ? null : `any(not(${pred}), ${joinCfgs(inner)})`, paths, pred, macroExport })
272
188
  }
273
189
 
274
- // An attribute that applies nothing in this build (a `cfg_attr` whose predicate can't hold).
275
190
  const INERT_ATTR = { cfg: null, paths: [], pred: null, includes: [] }
276
191
 
277
- // The visibility a `pub(…)` restriction spells, from the text between its parentheses: `crate`,
278
- // `self`, `super`, or `in <path>` (normalized to `in a::b`); a bare `pub` is `crate` -- the loader
279
- // never follows a path into another crate's tree, so `pub` and `pub(crate)` are one to it.
192
+ // The loader never follows a path into another crate's tree, so `pub` and `pub(crate)` are one to it.
280
193
  function parseVisibility(restriction) {
281
194
  const r = restriction.trim().replaceAll(/\s+/gu, ' ')
282
195
  if (r === 'crate' || r === 'self' || r === 'super') return r
@@ -284,12 +197,6 @@ function parseVisibility(restriction) {
284
197
  return 'crate'
285
198
  }
286
199
 
287
- // Flatten a `use` tree body (the text between `use` and `;`) into the paths it imports, as
288
- // `{ segments, absolute, spec, binding, glob }`: `a::{b::C, d::{E, F}}` → `a::b::C`, `a::d::E`,
289
- // `a::d::F`; a glob (`glob` true) or `self` inside a group names the group's own module; a leading
290
- // `::` marks the path absolute (an external crate). `spec` is the flat path as it would be
291
- // written; `binding` the name the import brings into scope (its last segment, or the `as` alias;
292
- // null for a glob or `_`).
293
200
  export function parseUseTree(body) {
294
201
  const tokens = [...body.matchAll(/::|[{},*]|\bas\b|(?:r#)?\w+/gu)].map((m) => m[0])
295
202
  const out = []
@@ -298,9 +205,8 @@ export function parseUseTree(body) {
298
205
  const next = () => tokens[pos++]
299
206
  const isIdent = (tok) => tok !== undefined && tok !== 'as' && /^(?:r#)?\w+$/u.test(tok)
300
207
  const emit = (segments, absolute, binding, glob) => {
301
- if (segments.length > 0) {
302
- out.push({ segments, absolute, spec: `${absolute ? '::' : ''}${segments.join('::')}`, binding: binding === '_' ? null : binding, glob })
303
- }
208
+ const spec = `${absolute ? '::' : ''}${segments.join('::')}`
209
+ if (segments.length > 0) out.push({ segments, absolute, spec, binding: binding === '_' ? null : binding, glob })
304
210
  }
305
211
  const parse = (prefix, absolute) => {
306
212
  let tok = peek()
@@ -343,7 +249,6 @@ export function parseUseTree(body) {
343
249
  next()
344
250
  alias = isIdent(peek()) ? next().replace(/^r#/u, '') : null
345
251
  }
346
- // `use a::{self, b}`: `self` names the group's own module.
347
252
  if (prefix.length > 0 && segments.length === prefix.length + 1 && segments.at(-1) === 'self') segments.pop()
348
253
  emit(segments, absolute, alias ?? segments.at(-1), false)
349
254
  }
@@ -351,112 +256,47 @@ export function parseUseTree(body) {
351
256
  return out
352
257
  }
353
258
 
354
- // Statically scan one file's items. An item whose cfg can never hold in the build (`#[cfg(test)]`,
355
- // `#[test]`, `#[cfg(doc)]`, a `#[cfg(feature = "x")]` with `x` off in the crate's resolved
356
- // `features`, a `#[cfg(windows)]` when `target` -- the build target's cfg set, see evalCfg -- says
357
- // otherwise) is skipped whole -- an inline `mod tests { … }` with everything in it, a `fn`'s
358
- // body, a `use` -- so dead modules aren't bundled and the dependencies only dead code reaches for
359
- // don't get pulled in. Returns
360
- // mods: external `mod` declarations `{ name, inlinePath, inlineDirs, cfg, conditional,
361
- // paths, noDefault, vis, macroUse, macro }` -- `inlinePath` is the chain of inline
362
- // `mod x { … }` blocks it sits in, `inlineDirs` the directories those stand for
363
- // (each its name, or its own `#[path = "…"]`, which may be empty), `vis` its
364
- // visibility (see imports), `macroUse` a `#[macro_use]` on it, `macro` the macro
365
- // invocation it sits in (if any), `noDefault` that a `#[cfg_attr(…, path)]` variant
366
- // always applies so the default file lookup never happens, `cfg` the
367
- // `#[cfg(…)]` predicate gating it (several → `all(…)`; null when ungated),
368
- // `conditional` marks one that may not exist as an item (a cfg the loader can't
369
- // decide, an inline ancestor so gated, or inside a macro invocation body), `paths`
370
- // the explicit file paths its attributes name (see parseAttr), minus the
371
- // `cfg_attr` variants whose predicate can't hold in the build, `at` where the
372
- // declaration is written and `offset` where it stands (textual macro scope: what a
373
- // mounted file sees) -- the same, unless it sits in a `macro_rules!` body
374
- // (`template`, that macro's name): then the macro's first bare invocation in
375
- // this file, Infinity for none;
376
- // refs: path references `{ spec, segments, absolute, inlinePath, fromUse, macroCall }` --
377
- // flattened `use` trees plus expression-position `crate::`/`self::`/`super::`/
378
- // `lead::…` paths (`macroCall`: one invoked, `$crate::name!(…)`);
379
- // externCrates: `extern crate x [as y];` as `{ name, alias, inlinePath }`;
380
- // defined: the items the file defines by name, `{ name, inlinePath, vis, cfg }`: a `struct`,
381
- // `enum`, `union`, `trait`, `type`, `fn`, `const` or `static` at module level (not
382
- // an `impl`'s or trait's associated item, nor a `macro_rules!` template's) -- what
383
- // a glob into the module brings in; `cfg` the predicate gating it (see mods);
384
- // bindings: names the file's `use` items and `extern crate … as` aliases bring into scope --
385
- // a path lead among them names an import, not a crate;
386
- // imports: the `use` items, and `extern crate` declarations as the imports they amount to
387
- // (`extern crate serde_core as s` ≡ `use ::serde_core as s`; `extern crate self as
388
- // x` ≡ `use crate as x`): `{ segments, absolute, binding, glob, inlinePath, vis,
389
- // cfg, macro }` -- what a path written in that module may be anchored on, and, as
390
- // far as `vis` reaches (null: private to the module; `crate`; `super`; `self`; `in
391
- // a::b`), what a path from another module may continue along, i.e. a re-export;
392
- // `cfg` gates it (see mods), `macro` is the invocation body it sits in, if any (one
393
- // in a `macro_rules!` template is the invoking module's, not this one's);
394
- // macros: `macro_rules!` definitions `{ name, exported, includes, calls, offset, template, gate }` -- an
395
- // exported one is `$crate::name!` from anywhere in the crate, and lives in this
396
- // file; `includes` are the include macros in its body, which rustc resolves
397
- // relative to each file that invokes the macro; one written in another
398
- // `macro_rules!`'s body (`template`, its name) is defined where that one is invoked
399
- // (`offset`: the first bare invocation in this file, Infinity for none), as is a
400
- // `mod` there;
401
- // invocations: the names of the macros invoked by bare name (`ready!(…)`) outside `macro_rules!`
402
- // templates -- for an edge to the file of a `macro_rules!` defined elsewhere in
403
- // the crate; `calls` has each name's invocation offsets, in order (textual scope
404
- // is decided at the call); a template's own bare calls are its definition's
405
- // `calls`, made wherever it is invoked;
406
- // includes: `include!` / `include_str!` / `include_bytes!` invocations outside macro
407
- // definitions, `{ kind, path, base, conditional }` -- a file to carry, named
408
- // relative to this one (`base` `file`) or to the package root (`manifest`, for
409
- // `concat!(env!("CARGO_MANIFEST_DIR"), …)`); `unfollowed` counts the ones whose
410
- // argument names no file the loader can follow (`concat!(env!("OUT_DIR"), …)`);
411
- // inlineModules: the paths of the inline `mod x { … }` blocks, for the module tree, and
412
- // `inlineModuleVis`, each path (`a::b`) → its visibility;
413
- // skipped: the template macros (see `templates` below) whose bodies were skipped.
414
- // A file whose inner `#![cfg(<pred>)]` can never hold is compiled empty: the scan is empty too.
415
- // `templates` names the macros whose bodies are token templates for another crate's code (quote!)
416
- // and are skipped whole: TEMPLATE_MACROS, less the ones the file's package defines itself. The
417
- // build (see evalCfg): `features` on for certain and `maybeFeatures` on maybe, `test`, the
418
- // platform's cfg set `target`; or `units`, one such build per way the file is compiled.
259
+ // An item whose cfg can never hold is skipped whole: dead modules aren't bundled, nor what only dead code reaches.
419
260
  export function scanRustItems(content, { features = null, maybeFeatures = null, test = false, target = null, units = null, templates = TEMPLATE_MACROS } = {}) {
420
261
  const env = units === null ? { features, maybeFeatures, test, target } : { units: units.map((u) => ({ ...u, test })) }
421
262
  const { code, masked } = lexRust(content)
422
263
  const n = masked.length
423
264
  const mods = []
424
- const uses = []
425
265
  const imports = []
426
266
  const macros = []
427
267
  const invocations = new Set()
428
- const calls = new Map() // macro name → the offsets of its bare invocations, in order
429
- const pathInvocations = new Set() // the macros invoked by path (`crate::m!()`), by their last segment
430
- const callSites = new Map() // macro name → each invocation, bare or by path: `{ offset, inlinePath, inlineDirs }`
268
+ const calls = new Map()
269
+ const pathInvocations = new Set()
270
+ const callSites = new Map()
431
271
  const includes = []
432
272
  const defined = []
433
- const skipped = new Set() // the template macros whose bodies were skipped
273
+ const skipped = new Set()
434
274
  let unfollowed = 0
435
- const useSpans = [] // [start, end) of each `use` item's body, parsed as a tree below
436
- const deadSpans = [] // [start, end) of each skipped test/doc-only item
275
+ const blanked = [] // `use` bodies and dead items, hidden from the expression-path scan
437
276
  const externCrates = []
438
277
  const bindings = new Set()
439
- const spans = [] // closed inline module blocks: { start, end, path, vis }
440
- const stack = [] // open inline modules: { name, dir, depth, conditional, start, vis }
441
- // `impl … { }` and `trait … { }` bodies, `{ start, end, depth }`: a `fn`, `const` or `type` at
442
- // their depth is an associated item, not one of the module's. `extern "C" { }` blocks the same
443
- // way: what they declare is the module's. Both are met in source order and nest or lie apart,
444
- // so each list is a stack: the ones the scan has left are popped off its end.
278
+ const spans = []
279
+ const stack = []
280
+ const refs = new Map()
281
+ // `fromUse`: the reliable signal that a lead names a crate. `macroCall` (`$crate::span!(…)`) names a macro, not a module.
282
+ const addRef = (spec, segments, absolute, ip, fromUse, macroCall) => {
283
+ const key = `${ip.join('::')}\0${spec}\0${macroCall ? '!' : ''}`
284
+ if (!refs.has(key)) refs.set(key, { spec, segments, absolute, inlinePath: ip, fromUse, macroCall })
285
+ }
286
+ // `impl`/`trait` bodies and `extern "C" { }` blocks nest or lie apart in source order: stacks popped once left.
445
287
  const assocBlocks = []
446
288
  const foreignBlocks = []
447
289
  const openBlocks = (blocks, at) => {
448
290
  while (blocks.length > 0 && blocks.at(-1).end < at) blocks.pop()
449
291
  return blocks
450
292
  }
451
- // The `macro_rules!` names this file defines: a `quote!` of its own is no template.
293
+ // A `quote!` the file defines itself is no template.
452
294
  const localMacros = new Set([...masked.matchAll(/\bmacro_rules!\s*([A-Za-z_]\w*)/gu)].map((m) => m[1]))
453
295
  let depth = 0
454
- let pending = [] // parsed outer attributes (parseAttr) waiting for their item
455
- let attrStart = 0 // where the first pending attribute begins: a dead item is dead from there
456
- let visNext = null // the previous token was a `pub` visibility (parseVisibility): the coming item has it
457
- // Open blocks a live `#[cfg]` gates -- `if #[cfg(unix)] { mod imp; }` in a `cfg_if!` body, a
458
- // `#[cfg(x)] mod m { mod a; }` -- as `{ depth, cfg }`: everything declared inside is gated too.
459
- // `scopeCfg` is their conjunction, kept as they open and close.
296
+ let pending = []
297
+ let attrStart = 0
298
+ let visNext = null
299
+ // Open blocks a live `#[cfg]` gates (a `cfg_if!` branch, a `#[cfg(x)] mod m { … }`), contents included.
460
300
  const cfgScopes = []
461
301
  let scopeCfg = null
462
302
  const openScope = (d, cfg) => {
@@ -468,30 +308,17 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
468
308
  while (cfgScopes.length > 0 && cfgScopes.at(-1).depth > d) cfgScopes.pop()
469
309
  scopeCfg = joinCfgs(cfgScopes.map((s) => s.cfg))
470
310
  }
471
- // A predicate's verdict in this scan's build (evalCfg), once per predicate: the open scopes'
472
- // conjunction is asked at every token inside them.
473
- const verdicts = new Map()
474
- const verdict = (pred) => cached(verdicts, pred, () => evalCfg(pred, env))
475
- // The cfg of a `#[cfg(…)] name! { … }`: it gates the invocation, so everything the body
476
- // declares (mio's `#[cfg(unix)] cfg_os_poll! { mod unix; }`); taken up by the body's `{`.
311
+ const verdict = memoized((pred) => evalCfg(pred, env))
312
+ // A `#[cfg(…)] name! { … }` gates all its body declares (mio's `#[cfg(unix)] cfg_os_poll! { mod unix; }`).
477
313
  let macroBodyCfg = null
478
- // A chain of cfg-gated branches, `if #[cfg(a)] { … } else if #[cfg(b)] { … } else { … }` (a
479
- // `cfg_if!` body): a branch applies only when the cfgs of those before it don't hold, so its
480
- // block is gated on `all(not(a), b)` -- dead outright once an earlier cfg is known to hold.
481
- // Open chains by the depth of their branch blocks (a `cfg_if!` inside a `cfg_if!` branch is a
482
- // chain of its own): depth → the cfgs of the branches so far.
314
+ // Block depth → the cfgs of a `cfg_if!` chain's branches so far: `else if #[cfg(b)]` is gated on `all(not(a), b)`.
483
315
  const chains = new Map()
484
- // The word right before `pos` (skipping whitespace) and where it starts, or null.
485
316
  const wordBefore = (pos) => {
486
- let k = pos - 1
487
- while (k >= 0 && WS_RE.test(masked[k])) k--
317
+ let k = skipWsBack(pos - 1)
488
318
  const end = k + 1
489
319
  while (k >= 0 && isWordChar(masked[k])) k--
490
320
  return k + 1 < end ? { word: masked.slice(k + 1, end), start: k + 1 } : null
491
321
  }
492
- // The cfg a `{` at `i` opens its block under, when the block is a branch of a cfg chain: its own
493
- // (`own`, from the attributes right after the `if`) and the negation of every earlier branch's;
494
- // null for any other block. Records the branch on its chain.
495
322
  const branchCfg = (i, own) => {
496
323
  const lead = wordBefore(pending.length > 0 ? attrStart : i)
497
324
  if (lead === null || (lead.word !== 'if' && lead.word !== 'else')) return null
@@ -501,60 +328,36 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
501
328
  chains.set(depth + 1, own === null ? negations : [...negations, own])
502
329
  return joinCfgs([...negations.map((c) => `not(${c})`), ...(own === null ? [] : [own])])
503
330
  }
504
- // After a branch block at `blockDepth` closed at `end`: its chain goes on only if an `else` follows.
505
331
  const endBranch = (end, blockDepth) => {
506
332
  if (chains.has(blockDepth) && readWord(skipWs(end)) !== 'else') chains.delete(blockDepth)
507
333
  }
508
- // End of the outermost macro invocation body being scanned (`m! { … }`, `m!( … )`,
509
- // `macro_rules! m { … }`). Its tokens are macro input: a `mod x;` there only becomes an item if
510
- // the macro emits it (cfg_if! does; serde_with's generate_guide! turns it into an inline module
511
- // documented from a .md file), so such declarations are followed when their file exists and
512
- // tolerated when it doesn't -- the same footing as a `#[cfg]`-gated one. `macroName` is that
513
- // macro's name: what stands for the cfg a `mod` it emits is under (tokio's `cfg_has_atomic_u64!
514
- // { mod imp; }` beside `cfg_not_has_atomic_u64! { mod imp; }`).
334
+ // The outermost macro invocation body being scanned. A `mod x;` there is an item only if the macro emits it,
335
+ // so it is tolerated when missing, like a `#[cfg]`-gated one.
515
336
  let macroUntil = -1
516
337
  let macroName = null
517
- // The `macro_rules!` being defined (its name) while inside its body: what it declares (serde's
518
- // `crate_root! { … mod de; … }`) appears where the macro is invoked, not where it is defined, so
519
- // such a `mod` takes the offset of the macro's first bare invocation in this file (`calls`).
338
+ // The top-level `macro_rules!` the scan is in: what its body declares appears where the macro is invoked.
520
339
  let templateName = null
521
- let templateMacro = null // the top-level `macro_rules!` whose body the scan is in: what its body calls is its own, nested definitions' too
522
- // The `macro_rules!` bodies the scan is in, outermost first, `{ macro, end, depth }`: what a
523
- // nested one's body calls, includes and declares is that one's, made where it is invoked (which
524
- // may be another directory than where the outer one is); `depth`, the inline modules around
525
- // its definition, which a `mod` its body declares is not in where it is invoked.
526
- const defs = []
527
- const innermostEntry = (at) => {
528
- while (defs.length > 0 && defs.at(-1).end < at) defs.pop()
529
- return defs.at(-1)
530
- }
340
+ let templateMacro = null
341
+ const defs = [] // the `macro_rules!` bodies the scan is in
342
+ const innermostEntry = (at) => openBlocks(defs, at).at(-1)
531
343
  const innermostDef = (at) => innermostEntry(at)?.macro ?? templateMacro
532
-
533
- // Whether the token at `at` sits in a `macro_rules!` body: a template, expanded where the
534
- // macro is invoked.
535
344
  const inTemplate = (at) => at < macroUntil && macroName === 'macro_rules'
536
- // Whether what is declared at `at` under `cfg` may not exist as an item: a cfg not known to
537
- // hold, an inline module so gated around it, or a macro invocation's body.
538
345
  const conditionalAt = (cfg, at) => (cfg !== null && verdict(cfg) !== true) || stack.some((s) => s.conditional) || at < macroUntil
539
346
  const inlinePath = () => stack.map((s) => s.name)
347
+ const inlineDirs = () => stack.map((s) => s.dir)
540
348
  const closeTo = (targetDepth, at) => {
541
349
  while (stack.length > 0 && stack.at(-1).depth > targetDepth) {
542
350
  const top = stack.pop()
543
351
  spans.push({ start: top.start, end: at, path: [...inlinePath(), top.name], vis: top.vis })
544
352
  }
545
353
  }
546
- // Whether an item at `at` (depth `d`) sits directly in an `impl`/`trait` body.
547
354
  const isAssociated = (at, d) => openBlocks(assocBlocks, at).some((b) => b.start < at && b.depth === d)
548
- // Whether an item at `at` is at its module's own level: the module body (a file's, an inline
549
- // module's), a `#[cfg]`-gated block or an `extern "C" { }` block there, or a macro invocation's
550
- // body (libc's `s! { … }`); not a `fn` body.
355
+ // The module body, a `#[cfg]`-gated or `extern "C"` block there, or a macro body (libc's `s! { … }`); not a `fn` body.
551
356
  const atModuleLevel = (at) => {
552
357
  if (at < macroUntil) return true
553
358
  const base = stack.length > 0 ? stack.at(-1).depth : 0
554
359
  return depth === base || cfgScopes.some((s) => s.depth === depth) || openBlocks(foreignBlocks, at).some((b) => b.start < at && b.depth === depth)
555
360
  }
556
- // The `{` opening the body of the item starting at `from` (an `impl`/`trait` header may hold
557
- // generics, bounds and a `where` clause), or -1 when a `;` ends it first.
558
361
  const bodyOpen = (from) => {
559
362
  let nest = 0
560
363
  for (let k = from; k < n; k++) {
@@ -579,18 +382,8 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
579
382
  while (at >= 0 && WS_RE.test(masked[at])) at--
580
383
  return at
581
384
  }
582
- // The end of the item starting at `from`, without leaving its enclosing block: through a
583
- // top-level `;` or its `{ … }` body, or -- for a field, variant or match arm (`itemLike`
584
- // false) -- a top-level `,`; a `}` or `)` closing the enclosing block is left for the main
585
- // loop. Parentheses/brackets nest (a tuple field's `(u8, u8)` comma isn't the field's end), and
586
- // so does a generic argument list -- a `<` right after a name or `::` (`HashMap<K, V>`, `f::<T>`)
587
- // up to its `>` (the `>` of `->` / `=>` is not one) -- so the comma in `Result<A, B>` isn't
588
- // either. An item (`fn f<'a, T>(…) -> R where T: Tr, { … }`, `impl<A, B> …`) never ends at a
589
- // comma at all. A dead statement or arm whose value is an `if` (`elseChain`: anything but a bare
590
- // `{ … }` block) takes its `else` branches with it; a dead bare block does not: in `cfg_if! { if
591
- // #[cfg(a)] { … } else { … } }` the `else` is what applies when the cfg doesn't.
592
- // A `let` (`toSemicolon`) runs to its `;` whatever blocks its initializer holds.
593
- const skipItem = (from, itemLike, { elseChain = false, toSemicolon = false } = {}) => {
385
+ // A dead `if` (`elseChain`) takes its `else`s along, a bare block doesn't; a `let` runs to its `;`.
386
+ const skipItem = (from, itemLike, { elseChain, toSemicolon }) => {
594
387
  let nest = 0
595
388
  let angle = 0
596
389
  for (let k = from; k < n; k++) {
@@ -634,29 +427,24 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
634
427
  const close = matchClose(masked, open)
635
428
  const attr = parseAttr(code.slice(open + 1, close), masked.slice(open + 1, close))
636
429
  if (outer) {
637
- // `#[cfg_attr(<pred>, derive(serde::Serialize))]` with pred never holding applies nothing:
638
- // the paths in its text are dead code too, and so is whatever else it would apply.
430
+ // A `cfg_attr` whose predicate never holds applies nothing: the paths in its text are dead code too.
639
431
  const inert = attr.pred !== null && verdict(attr.pred) === false
640
- if (inert) deadSpans.push([i, close + 1])
432
+ if (inert) blanked.push([i, close + 1])
641
433
  if (pending.length === 0) attrStart = i
642
434
  pending.push(inert ? INERT_ATTR : attr)
643
435
  } else {
644
- // An inner `#![…]` attribute applies to the enclosing module, not to the next item. A
645
- // `#![cfg(<pred>)]` that can't hold empties that module: the whole file at the top, else
646
- // the inline module it opens.
647
- // (An inner attribute inside a macro invocation's body is the macro's business.)
436
+ // A `#![cfg]` that can't hold empties the enclosing module (outside a macro body): at the top, the file.
648
437
  if (attr.cfg !== null && verdict(attr.cfg) === false && i >= macroUntil) {
649
438
  if (depth === 0) return { mods: [], refs: [], externCrates: [], bindings: new Set(), imports: [], macros: [], invocations: new Set(), calls: new Map(), pathInvocations: new Set(), callSites: new Map(), includes: [], unfollowed: 0, defined: [], inlineModules: [], inlineModuleVis: new Map(), skipped }
650
439
  const top = stack.at(-1)
651
440
  if (top !== undefined && top.depth === depth) {
652
441
  const end = matchClose(masked, top.start - 1)
653
- deadSpans.push([i, end])
442
+ blanked.push([i, end])
654
443
  pending = []
655
444
  i = end
656
445
  continue
657
446
  }
658
447
  }
659
- // `#![doc = include_str!("../README.md")]`: the module's own include, applied here.
660
448
  const conditional = conditionalAt(null, i)
661
449
  for (const inc of attr.includes) includes.push({ ...inc, conditional })
662
450
  pending = []
@@ -664,30 +452,20 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
664
452
  i = close + 1
665
453
  continue
666
454
  }
667
- // The cfg gating the item the pending attributes belong to (several `#[cfg]`s all apply,
668
- // and so does every enclosing block's). `pub` is visibility, not the item: its attributes
669
- // stay pending for the keyword after it.
455
+ // A `pub`'s attributes stay pending for the keyword after it.
670
456
  const word = readWord(i)
671
457
  const attrCfg = word === 'pub' ? null : joinCfgs(pending.map((a) => a.cfg).filter((c) => c !== null))
672
- // A `{` opening a branch of a cfg chain is gated on the earlier branches' cfgs not holding too.
673
458
  const own = ch === '{' ? branchCfg(i, attrCfg) ?? attrCfg : attrCfg
674
459
  const cfg = own === null ? scopeCfg : joinCfgs([...cfgScopes.map((s) => s.cfg), own])
675
- // An item gated on a cfg that never holds in the build is dead code for the bundle: skip it
676
- // whole -- from its first attribute (a `#[derive(serde::Serialize)]` on it names nothing
677
- // live) through a declaration's `;`, a field's or variant's `,`, or a body's or block's `}`
678
- // -- without recording anything in it, whatever token starts it: a keyword or a name, a
679
- // `{ … }` block statement, a `(a, b)` / `[a, ..]` pattern or tuple type, a `&x`, `*x` or literal.
680
460
  if (cfg !== null && verdict(cfg) === false) {
681
461
  pending = []
682
462
  visNext = null
683
463
  const end = skipItem(word === null ? i : i + word.length, word !== null && ITEM_KEYWORDS.has(word), { elseChain: ch !== '{', toSemicolon: word === 'let' })
684
- deadSpans.push([attrStart, end])
464
+ blanked.push([attrStart, end])
685
465
  if (ch === '{') endBranch(end, depth + 1)
686
466
  i = end
687
467
  continue
688
468
  }
689
- // The item is live, so the includes its attributes name (`#[doc = include_str!("docs/x.md")]`)
690
- // are too; a `pub` keeps them pending for the keyword after it.
691
469
  if (word !== 'pub' && pending.length > 0) {
692
470
  const conditional = conditionalAt(cfg, i)
693
471
  for (const a of pending) {
@@ -695,105 +473,82 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
695
473
  a.includes = []
696
474
  }
697
475
  }
698
- if (ch === '{') {
699
- depth++
700
- const gate = own ?? macroBodyCfg
701
- macroBodyCfg = null
702
- if (gate !== null) openScope(depth, gate)
703
- pending = []
704
- i++
705
- continue
706
- }
707
- if (ch === '}') {
708
- depth--
709
- closeTo(depth, i)
710
- closeScopes(depth)
711
- endBranch(i + 1, depth + 1)
476
+ if (word === null) {
477
+ if (ch === '{') {
478
+ depth++
479
+ const gate = own ?? macroBodyCfg
480
+ macroBodyCfg = null
481
+ if (gate !== null) openScope(depth, gate)
482
+ } else if (ch === '}') {
483
+ depth--
484
+ closeTo(depth, i)
485
+ closeScopes(depth)
486
+ endBranch(i + 1, depth + 1)
487
+ } else visNext = null
712
488
  pending = []
713
489
  i++
714
490
  continue
715
491
  }
716
492
  const vis = visNext
717
493
  visNext = null
718
- if (word === null) {
719
- pending = []
720
- i++
721
- continue
722
- }
723
- // `name!` followed by a bracket opens a macro invocation body (`macro_rules! name` too).
724
494
  const bang = skipWs(i + word.length)
725
- const pathQualified = masked[i - 1] === ':' && masked[i - 2] === ':'
726
495
  if (masked[bang] === '!') {
727
496
  let open = skipWs(bang + 1)
728
- let definition = null // the `macro_rules!` this word defines
497
+ let definition = null
729
498
  if (word === 'macro_rules') {
730
499
  const name = readWord(open)
731
500
  if (name !== null) {
732
501
  open = skipWs(open + name.length)
733
502
  const gate = GATE_MACRO_RE.test(name) && masked[open] === '{' ? gatePredicate(code, masked, open, matchClose(masked, open)) : undefined
734
- macros.push({ name, exported: pending.some((a) => a.macroExport === true), includes: [], calls: new Set(), pathCalls: new Set(), offset: i, at: i, template: inTemplate(i) ? templateName : null, gate, cfg, macro: i < macroUntil ? macroName : null })
735
- definition = macros.at(-1)
503
+ definition = { name, exported: pending.some((a) => a.macroExport === true), includes: [], calls: new Set(), pathCalls: new Set(), offset: i, at: i, template: inTemplate(i) ? templateName : null, gate, cfg, macro: i < macroUntil ? macroName : null }
504
+ macros.push(definition)
736
505
  if (i >= macroUntil) {
737
506
  templateName = name
738
- templateMacro = macros.at(-1)
507
+ templateMacro = definition
739
508
  }
740
509
  }
741
510
  }
742
511
  if (masked[open] === '{' || masked[open] === '(' || masked[open] === '[') {
743
512
  const close = matchClose(masked, open)
744
- if (definition !== null) {
745
- innermostDef(i)
746
- defs.push({ macro: definition, end: close, depth: stack.length })
747
- }
748
- if (templates.has(word) && !localMacros.has(word)) {
749
- // serde_derive's `quote! { use #path as _serde; … _serde::__private::Ok(…) }`, bare or as
750
- // `quote::quote! { … }`: skipped whole. (A crate's own `macro_rules! quote` is no template.)
751
- deadSpans.push([i, close + 1])
752
- skipped.add(word)
513
+ // A `macro_rules!` with no name defines nothing (rustc refuses it); a template's body is another crate's code.
514
+ const unnamed = word === 'macro_rules' && definition === null
515
+ if (unnamed || (templates.has(word) && !localMacros.has(word))) {
516
+ if (!unnamed) skipped.add(word)
517
+ blanked.push([i, close + 1])
753
518
  pending = []
754
519
  i = close + 1
755
520
  continue
756
521
  }
522
+ if (definition !== null) {
523
+ openBlocks(defs, i)
524
+ defs.push({ macro: definition, end: close, depth: stack.length })
525
+ }
757
526
  if (i >= macroUntil) macroName = word
758
- // (A cfg on a `macro_rules!` definition gates the definition, not what its template
759
- // declares wherever it is invoked -- an approximation kept from before.)
760
- if (own !== null && masked[open] === '{' && word !== 'macro_rules') macroBodyCfg = own
527
+ // A cfg on a `macro_rules!` gates the definition, not what its template declares where invoked.
761
528
  if (word !== 'macro_rules') {
762
- // Invoked by bare name; `$crate::name!` / `serde_json::json!` is a path (a ref). One in a
763
- // `macro_rules!` template is the template's (`calls` on the definition): rustc resolves
764
- // it where the macro is invoked, not where it is written -- and a recursive arm's
765
- // `m!(…)` is no invocation of this file's.
766
- // A call by path (`crate::m!()`, `$crate::m!()`, `dep::m!()`) is no name to resolve in
767
- // scope, but an invocation of a macro of that name all the same, for where what its
768
- // template declares and includes is made (`pathInvocations`, `pathCalls`); each call
769
- // outside templates is also kept with the inline modules around it (`callSites`).
770
- if (inTemplate(i) && templateMacro !== null) (pathQualified ? innermostDef(i).pathCalls : innermostDef(i).calls).add(word)
529
+ if (own !== null && masked[open] === '{') macroBodyCfg = own
530
+ // A call in a template is the definition's, made where that is invoked.
531
+ const def = inTemplate(i) && templateMacro !== null ? innermostDef(i) : null
532
+ const pathQualified = masked[i - 1] === ':' && masked[i - 2] === ':'
533
+ if (def !== null) (pathQualified ? def.pathCalls : def.calls).add(word)
534
+ else if (pathQualified) pathInvocations.add(word)
771
535
  else {
772
- if (pathQualified) pathInvocations.add(word)
773
- else {
774
- invocations.add(word)
775
- ;(calls.get(word) ?? calls.set(word, []).get(word)).push(i)
776
- }
777
- ;(callSites.get(word) ?? callSites.set(word, []).get(word)).push({ offset: i, inlinePath: inlinePath(), inlineDirs: stack.map((s) => s.dir) })
536
+ invocations.add(word)
537
+ upsert(calls, word, () => []).push(i)
778
538
  }
539
+ if (def === null) upsert(callSites, word, () => []).push({ offset: i, inlinePath: inlinePath(), inlineDirs: inlineDirs() })
779
540
  if (INCLUDE_MACROS.has(word)) {
780
541
  const text = code.slice(open + 1, close)
781
542
  const arg = includeArg(text)
782
- const conditional = conditionalAt(cfg, i)
783
- // Inside a `macro_rules!` body the path is relative to whichever file invokes the
784
- // macro: recorded on the definition, resolved per invoking file. Build output
785
- // (`concat!(env!("OUT_DIR"), …)`) is nobody's to follow; any other unreadable argument
786
- // is counted, for a warning.
543
+ // Build output (`OUT_DIR`) is nobody's to follow; any other unreadable argument is counted.
787
544
  if (arg === null) unfollowed += /\bOUT_DIR\b/u.test(text) ? 0 : 1
788
- else if (inTemplate(i) && templateMacro !== null) innermostDef(i).includes.push({ kind: word, ...arg })
789
- else includes.push({ kind: word, ...arg, conditional })
545
+ else if (def !== null) def.includes.push({ kind: word, ...arg })
546
+ else includes.push({ kind: word, ...arg, conditional: conditionalAt(cfg, i) })
790
547
  }
791
548
  }
792
549
  macroUntil = Math.max(macroUntil, close)
793
550
  }
794
551
  }
795
- // An `impl`/`trait` body holds associated items, an `extern "C" { }` block the module's own
796
- // (an `extern "C" fn f() { }` is a fn, with a body of its own).
797
552
  if (word === 'impl' || word === 'trait') {
798
553
  const open = bodyOpen(i + word.length)
799
554
  if (open !== -1) assocBlocks.push({ start: open, end: matchClose(masked, open), depth: depth + 1 })
@@ -802,24 +557,16 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
802
557
  if (masked[k] === '"') k = skipWs(masked.indexOf('"', k + 1) + 1)
803
558
  if (masked[k] === '{') foreignBlocks.push({ start: k, end: matchClose(masked, k), depth: depth + 1 })
804
559
  }
805
- // An item the module defines by name (`pub struct sigset_t`, inside libc's `s! { … }` or an
806
- // `extern "C" { }` block too), at the module's own level -- not a `fn`'s local item, nor an
807
- // `impl`/`trait` body's associated one, nor a `macro_rules!` template's (that is some invoking
808
- // module's item); `const fn` / `static mut` / lazy_static's `static ref` are not items named
809
- // `fn` / `mut` / `ref`.
560
+ // Not a `*const T` pointer type; `const fn`, `static mut` and `static ref` define no `fn`, `mut` or `ref`.
810
561
  if (DEFINING_KEYWORDS.has(word) && atModuleLevel(i) && !isAssociated(i, depth) && !inTemplate(i) && !(word === 'const' && masked[skipWsBack(i - 1)] === '*')) {
811
562
  let at = skipWs(i + word.length)
812
563
  if (masked.startsWith('mut ', at) || masked.startsWith('ref ', at)) at = skipWs(at + 3)
813
564
  if (masked.startsWith('r#', at)) at += 2
814
565
  const name = readWord(at)
815
566
  const next = name === null ? null : masked[skipWs(at + name.length)]
816
- // `ns`: a `fn`, `const` or `static` lives in the value namespace and can't lead a path
817
- // (`*const libc::c_char`, a raw pointer type, is no item at all; a `fn log` beside
818
- // `use log::info` hides no crate).
819
567
  if (name !== null && !ITEM_KEYWORDS.has(name) && (word !== 'union' || next === '{' || next === '<')) defined.push({ name, inlinePath: inlinePath(), vis, cfg, macro: i < macroUntil ? macroName : null, ns: VALUE_KEYWORDS.has(word) ? 'value' : 'type' })
820
568
  }
821
569
  if (word === 'pub') {
822
- // Visibility sits between an item's attributes and its keyword; `pub(crate)` etc. included.
823
570
  i = skipWs(i + 3)
824
571
  visNext = 'crate'
825
572
  if (masked[i] === '(') {
@@ -841,18 +588,14 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
841
588
  const k = skipWs(j + name.length)
842
589
  const attrs = pending
843
590
  pending = []
844
- // Dead cfgs were skipped above; a decidable-true one (`not(test)`) is as firm as no cfg at all.
845
591
  const conditional = conditionalAt(cfg, i)
846
592
  if (masked[k] === ';') {
847
- // rustc applies the first `#[path]` / `#[cfg_attr(<pred>, path = …)]` whose predicate holds.
848
- // A variant whose predicate can't hold names nothing in this build; one whose predicate
849
- // holds ends the list -- outright the `#[path]` when nothing undecided precedes it -- and
850
- // either way the default `<name>.rs` lookup never happens.
593
+ // rustc applies the first `#[path]` whose predicate holds; then the default lookup is off.
851
594
  const live = attrs.flatMap((a) => a.paths).filter((p) => p.cfg === null || verdict(p.cfg) !== false)
852
595
  const applies = live.findIndex((p) => p.cfg === null || verdict(p.cfg) === true)
853
596
  const paths = applies === -1 ? live : (applies === 0 ? [{ path: live[0].path, cfg: null }] : live.slice(0, applies + 1))
854
597
  mods.push({
855
- name, inlinePath: inlinePath(), inlineDirs: stack.map((s) => s.dir), cfg, conditional, paths, noDefault: applies !== -1, vis,
598
+ name, inlinePath: inlinePath(), inlineDirs: inlineDirs(), cfg, conditional, paths, noDefault: applies !== -1, vis,
856
599
  macroUse: attrs.some((a) => a.macroUse === true), macro: i < macroUntil ? macroName : null, offset: i, at: i,
857
600
  template: inTemplate(i) ? innermostDef(i).name : null, templateDepth: inTemplate(i) ? innermostEntry(i)?.depth ?? 0 : 0,
858
601
  })
@@ -860,8 +603,7 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
860
603
  continue
861
604
  }
862
605
  if (masked[k] === '{') {
863
- // `#[path = "dir"] mod m { mod a; }` finds `a` under `dir/`, not `m/`; solana-program's
864
- // `#[path = ""] mod non_bpf_modules { mod account_keys; }` right beside the file.
606
+ // `#[path = "dir"] mod m { mod a; }` finds `a` under `dir/`, not `m/`.
865
607
  const dir = attrs.flatMap((a) => a.paths).find((p) => p.cfg === null)?.path ?? name
866
608
  depth++
867
609
  if (own !== null) openScope(depth, own)
@@ -876,11 +618,8 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
876
618
  const end = masked.indexOf(';', i + 3)
877
619
  const stop = end === -1 ? n : end
878
620
  const body = code.slice(i + 3, stop)
879
- // A `use` item's tree starts with a name, `::`, `{` or -- in a `macro_rules!` template --
880
- // `$crate`. Anything else is the word in macro input (syn's `Token![use]`), and a tree
881
- // interpolating a metavariable (`use $m::X;`, quote's `use #path as _serde;`; a raw
882
- // identifier's `r#` is no interpolation) is a template for code the loader can't place:
883
- // neither is an import of this file.
621
+ // Not an import of this file: the word in macro input (`Token![use]`), or one interpolating a
622
+ // metavariable (`use $m::X;`, quote's `#path`; `r#` is none), which the loader can't place.
884
623
  if (!/^\s*[A-Za-z_{:$]/u.test(body) || /(?<!\br)#|\$(?!crate\b)/u.test(body)) {
885
624
  pending = []
886
625
  i += 3
@@ -888,15 +627,15 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
888
627
  }
889
628
  const ip = inlinePath()
890
629
  const macro = i < macroUntil ? macroName : null
891
- // `use $crate::…` in a `macro_rules!` template imports into whichever module invokes the macro.
630
+ // `use $crate::…` in a template imports into whichever module invokes the macro.
892
631
  const template = macro === 'macro_rules' && /\$crate\b/u.test(body)
893
632
  for (const p of parseUseTree(body)) {
894
- uses.push({ ...p, inlinePath: ip })
633
+ addRef(p.spec, p.segments, p.absolute, ip, true, false)
895
634
  const im = { segments: p.segments, absolute: p.absolute, binding: p.binding, glob: p.glob, inlinePath: ip, vis, cfg, macro, template }
896
635
  imports.push(im)
897
636
  if (hidesCrate(im)) bindings.add(p.binding)
898
637
  }
899
- useSpans.push([i, stop])
638
+ blanked.push([i, stop])
900
639
  pending = []
901
640
  i = stop + 1
902
641
  continue
@@ -904,65 +643,44 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
904
643
  if (word === 'extern') {
905
644
  EXTERN_CRATE_RE.lastIndex = i
906
645
  const m = EXTERN_CRATE_RE.exec(masked)
907
- const macroUse = pending.some((a) => a.macroUse === true)
908
- pending = []
909
646
  if (m) {
910
647
  const ip = inlinePath()
911
- externCrates.push({ name: m[1], alias: m[2] ?? null, inlinePath: ip, macroUse })
648
+ externCrates.push({ name: m[1], alias: m[2] ?? null, inlinePath: ip, macroUse: pending.some((a) => a.macroUse === true) })
649
+ pending = []
912
650
  if (m[2] !== undefined) bindings.add(m[2])
913
- // `extern crate self as x;` names this crate's root; any other, a crate by its own name.
914
651
  const selfCrate = m[1] === 'self'
915
652
  imports.push({ segments: [selfCrate ? 'crate' : m[1]], absolute: !selfCrate, binding: m[2] ?? m[1], glob: false, inlinePath: ip, vis, cfg, macro: i < macroUntil ? macroName : null })
916
653
  i = EXTERN_CRATE_RE.lastIndex
917
654
  continue
918
655
  }
919
- i += word.length
920
- continue
921
656
  }
922
657
  pending = []
923
658
  i += word.length
924
659
  }
925
660
  closeTo(0, n)
926
- // A `mod` or `macro_rules!` a `macro_rules!` template declares stands where the macro is first
927
- // invoked here (serde's `crate_root! { … macro_rules! tri { … } … mod de; … }`).
661
+ // What a template declares stands where the macro is first invoked here (serde's `crate_root! { … mod de; … }`).
928
662
  for (const m of [...mods, ...macros]) if (m.template !== null) m.offset = calls.get(m.template)?.[0] ?? Infinity
929
663
 
930
- // Innermost inline module enclosing an offset (spans nest, so the longest path wins).
664
+ // Spans nest, so the longest path is the innermost.
931
665
  const inlineAt = (offset) => {
932
- let best = null
933
- for (const s of spans) {
934
- if (s.start <= offset && offset < s.end && (best === null || s.path.length > best.path.length)) best = s
935
- }
936
- return best ? best.path : []
666
+ let best = []
667
+ for (const s of spans) if (s.start <= offset && offset < s.end && s.path.length > best.length) best = s.path
668
+ return best
937
669
  }
938
- const refs = new Map()
939
- // `fromUse` marks a path written in a `use` item -- the reliable signal that its lead names a
940
- // crate (an expression path's lead is as likely a module or an imported item). `macroCall`
941
- // marks an invoked path (`$crate::span!(…)`): it names a macro, not the module of that name.
942
- const addRef = (spec, segments, absolute, ip, fromUse, macroCall) => {
943
- const key = `${ip.join('::')}\0${spec}\0${macroCall ? '!' : ''}`
944
- if (!refs.has(key)) refs.set(key, { spec, segments, absolute, inlinePath: ip, fromUse, macroCall })
945
- }
946
- for (const u of uses) addRef(u.spec, u.segments, u.absolute, u.inlinePath, true, false)
947
- // Expression-position paths: scan everything outside `use` items (the tree parser owns those;
948
- // a regex over `use syn::{parse::Parse}` would take `parse::Parse` for a local module path) and
949
- // outside skipped test/doc-only items (a `quickcheck::quickcheck(…)` in a `#[test]` fn body
950
- // must not pull the vendored dev-dependency in): blank those spans, keeping offsets.
670
+ // Expression paths outside `use`s and dead code (a test's `quickcheck::quickcheck(…)` mustn't pull the dev-dependency in).
951
671
  let exprs = ''
952
672
  let at = 0
953
- for (const [start, end] of [...useSpans, ...deadSpans].toSorted((a, b) => a[0] - b[0])) {
673
+ for (const [start, end] of blanked.toSorted((a, b) => a[0] - b[0])) {
954
674
  const from = Math.max(start, at)
955
675
  if (end <= from) continue
956
676
  exprs += masked.slice(at, from) + ' '.repeat(end - from)
957
677
  at = end
958
678
  }
959
679
  exprs += masked.slice(at)
960
- // Keyword paths (`crate::`/`self::`/`super::`) first, then paths led by a module or crate name.
961
680
  const keywordPaths = []
962
681
  const leadPaths = []
963
682
  for (const m of exprs.matchAll(LEAD_PATH_RE)) {
964
- // `$trait::fmt(…)` in a `macro_rules!` template: the lead is a metavariable, not a name;
965
- // `$crate::…` is the one interpolation that names something (this crate).
683
+ // A metavariable lead (`$trait::fmt(…)`) names nothing; `$crate::…` names this crate.
966
684
  if (exprs[m.index - 1] === '$' && m[1] !== 'crate') continue
967
685
  if (PATH_KEYWORDS.has(m[1])) keywordPaths.push(m)
968
686
  else if (!NON_CRATE_LEADS.has(m[1])) leadPaths.push(m)
@@ -979,16 +697,10 @@ export function scanRustItems(content, { features = null, maybeFeatures = null,
979
697
  return { mods, refs: [...refs.values()], externCrates, bindings, imports, macros, invocations, calls, pathInvocations, callSites, includes, unfollowed, defined, inlineModules, inlineModuleVis, skipped }
980
698
  }
981
699
 
982
- // Whether a `use` import binds a name that is not the crate of that name: `use std::io;` binds
983
- // `io`, but `use serde_json;` / `use rand::{self, Rng};` bind the crate itself under its own name.
700
+ // Whether a `use` binds a name that is not the crate of that name: `use std::io;` does, `use serde_json;` doesn't.
984
701
  const hidesCrate = (im) => im.binding !== null && (im.segments.length > 1 || im.binding !== im.segments[0])
985
702
 
986
- // --- Module files -----------------------------------------------------------------------
987
-
988
- // Directory holding this file's submodules, per Rust's path rules: siblings for crate roots,
989
- // mod.rs and files loaded through `#[path = …]` (rustc treats those like mod.rs), else under a
990
- // `<stem>/` subdir. `root` marks a crate root by role (an entry such as `src/bin/tool.rs` or
991
- // `tests/it.rs`) rather than by name.
703
+ // The directory of a file's submodules: its own for mod.rs, a crate root and (`root`) any file owning it; else `<stem>/`.
992
704
  export function getModuleDir(filePath, { root = false } = {}) {
993
705
  const lastSlash = filePath.lastIndexOf('/')
994
706
  const dir = lastSlash === -1 ? '' : filePath.slice(0, lastSlash)
@@ -998,8 +710,6 @@ export function getModuleDir(filePath, { root = false } = {}) {
998
710
  return dir ? `${dir}/${stem}` : stem
999
711
  }
1000
712
 
1001
- // Whether `file` owns its directory for submodule lookup: a crate root by role, or a file a
1002
- // `#[path]` attribute loaded (`pathLoaded`).
1003
713
  const ownsDir = (file, { roots, pathLoaded }) => roots?.has(file) === true || pathLoaded?.has(file) === true
1004
714
 
1005
715
  function firstFile(candidates, { knownSources, baseDir }) {
@@ -1008,19 +718,11 @@ function firstFile(candidates, { knownSources, baseDir }) {
1008
718
  return null
1009
719
  }
1010
720
 
1011
- // Resolve `mod <name>;` declared in `fromFile` (inside the inline modules `inlinePath`) to its
1012
- // file, trying `<dir>/<name>.rs` then `<dir>/<name>/mod.rs`. `roots` are the files walked as crate
1013
- // roots (the entries) and `pathLoaded` the files a `#[path]` loaded: their submodules are
1014
- // siblings. Both also get the non-root `<stem>/` rule as a fallback: a glob-listed module file
1015
- // still resolves, and so does a file that a plain `mod` mounts as well as a `#[path]` (each mount
1016
- // is its own module to rustc; the loader keeps one resolution, whichever file exists).
721
+ // A file owning its directory tries the `<stem>/` rule after, so a glob-listed module file still resolves.
1017
722
  export function resolveModPath(modName, fromFile, { knownSources, baseDir, roots, pathLoaded, inlinePath = [], boundary = null } = {}) {
1018
723
  const owns = ownsDir(fromFile, { roots, pathLoaded })
1019
- const dirs = [getModuleDir(fromFile, { root: owns })]
1020
- if (owns) dirs.push(getModuleDir(fromFile)) // the same dir for main.rs/lib.rs/mod.rs
1021
- for (const dir of new Set(dirs)) {
1022
- // An inline module's `#[path = "…"]` may climb (`..`): normalized, and held to `boundary`
1023
- // like any explicit path (resolveExplicitModPath).
724
+ for (const dir of new Set([getModuleDir(fromFile, { root: owns }), getModuleDir(fromFile)])) {
725
+ // An inline module's `#[path = "…"]` may climb (`..`): held to `boundary` like any explicit path.
1024
726
  const base = normalizeRel(dir, [...inlinePath, modName].filter(Boolean).join('/'))
1025
727
  if (base === null || (boundary !== null && !withinDir(base, boundary))) continue
1026
728
  const found = firstFile([`${base}.rs`, `${base}/mod.rs`], { knownSources, baseDir })
@@ -1029,9 +731,7 @@ export function resolveModPath(modName, fromFile, { knownSources, baseDir, roots
1029
731
  return null
1030
732
  }
1031
733
 
1032
- // Resolve a `#[path = "…"]` target the way rustc does: relative to the declaring file's directory,
1033
- // or -- inside inline modules -- to the file's module dir plus the inline module names. Absolute
1034
- // or root-escaping paths resolve to nothing (a read would refuse them anyway).
734
+ // Inside inline modules, relative to the file's module dir plus theirs; root-escaping paths resolve to nothing.
1035
735
  export function resolveExplicitModPath(explicitPath, fromFile, { knownSources, baseDir, roots, pathLoaded, inlinePath = [], boundary = null } = {}) {
1036
736
  const dir = inlinePath.length === 0
1037
737
  ? posix.dirname(fromFile)
@@ -1041,34 +741,23 @@ export function resolveExplicitModPath(explicitPath, fromFile, { knownSources, b
1041
741
  return firstFile([rel], { knownSources, baseDir })
1042
742
  }
1043
743
 
1044
- // Whether project-relative `rel` lies in directory `dir` (`.`: anywhere in the bundle root).
1045
744
  const withinDir = (rel, dir) => dir === '.' || rel === dir || rel.startsWith(`${dir}/`)
1046
745
 
1047
- // Whether the file at project-relative `rel` really lies in directory `within` (`.`: anywhere in
1048
- // the bundle root, which assertRealPathWithinBase holds it to): a vendored crate's file linked out
1049
- // of its package -- to the project's `.env`, say -- doesn't, and is refused like an include naming
1050
- // the target outright, warned as `who`'s. `realDirs` keeps the directories' real paths.
746
+ // A vendored crate's file linked out of its package (to the project's `.env`, say) is refused.
1051
747
  export function withinRealDir(baseDir, rel, within, { who = 'loader.rust', realDirs = new Map() } = {}) {
1052
748
  if (within === '.') return true
1053
- if (!realDirs.has(within)) realDirs.set(within, realpathSync(join(baseDir, within)))
1054
- const back = toPosix(relative(realDirs.get(within), realpathSync(join(baseDir, rel))))
749
+ const back = toPosix(relative(upsert(realDirs, within, () => realpathSync(join(baseDir, within))), realpathSync(join(baseDir, rel))))
1055
750
  if (!relativeEscapes(back)) return true
1056
751
  console.warn(`[${who}] Refusing file outside its package: ${rel} (a link out of ${within})`)
1057
752
  return false
1058
753
  }
1059
754
 
1060
- // The directory a file's `#[path]`, include and build-script paths may reach into: a published
1061
- // crate never legitimately names a file outside its own package, so a file of a vendored one is
1062
- // held to that package's directory (`vendor/<crate>`); the project's own code -- the root package,
1063
- // workspace members, path dependencies -- may reach anywhere in the bundle root (`.`).
755
+ // A published crate never names a file outside its package; the project's own code may reach the bundle root.
1064
756
  export function boundaryOf(file, ctx) {
1065
757
  return ctx?.isVendored(file) === true ? ctx.packageInfo(file).dir : '.'
1066
758
  }
1067
759
 
1068
- // Resolve an include macro's argument (scanRustItems' `{ kind, path, base }`) named in `file` to a
1069
- // project-relative path: relative to the file, or to its package's root for a
1070
- // `concat!(env!("CARGO_MANIFEST_DIR"), …)` path; null when it escapes the bundle root, or the
1071
- // package when the file is a vendored crate's (boundaryOf).
760
+ // Null when the include escapes the bundle root, or a vendored crate's package.
1072
761
  function includeTarget(inc, file, ctx) {
1073
762
  const dir = inc.base === 'manifest' ? ctx?.packageInfo(file)?.dir ?? null : posix.dirname(file)
1074
763
  if (dir === null) return null
@@ -1080,13 +769,7 @@ function includeTarget(inc, file, ctx) {
1080
769
  return rel
1081
770
  }
1082
771
 
1083
- // Every file a `mod` declaration can denote, as `{ cfg, file, explicit }`: an unconditional
1084
- // `#[path]` names one outright; otherwise each `#[cfg_attr(<pred>, path = …)]` names one under
1085
- // its predicate (`cfg`), and the default `<name>.rs`/`<name>/mod.rs` lookup is the fallback (`cfg`
1086
- // null); the scanner has dropped those whose predicate can't hold in the build it scanned under.
1087
- // `explicit` marks a file a `#[path]` named (its own submodules then sit beside it). The inline
1088
- // modules the declaration sits in contribute their directories (`inlineDirs`: an inline module's
1089
- // own `#[path]` replaces its name, an empty one adds nothing), else their names.
772
+ // An unconditional `#[path]`'s file; else each `#[cfg_attr(<pred>, path)]`'s under its predicate, then the default lookup's.
1090
773
  export function resolveModDecl(decl, fromFile, opts = {}) {
1091
774
  const o = { ...opts, inlinePath: decl.inlineDirs ?? decl.inlinePath }
1092
775
  const unconditional = decl.paths.find((p) => p.cfg === null)
@@ -1099,65 +782,29 @@ export function resolveModDecl(decl, fromFile, opts = {}) {
1099
782
  const file = resolveExplicitModPath(path, fromFile, o)
1100
783
  if (file && !out.some((x) => x.file === file)) out.push({ cfg, file, explicit: true })
1101
784
  }
1102
- // A variant that always applies (`decl.noDefault`) leaves no build for the default lookup.
785
+ // A variant that always applies leaves no build for the default lookup.
1103
786
  const fallback = decl.noDefault === true ? null : resolveModPath(decl.name, fromFile, o)
1104
787
  if (fallback && !out.some((x) => x.file === fallback)) out.push({ cfg: null, file: fallback, explicit: false })
1105
788
  return out
1106
789
  }
1107
790
 
1108
- // Resolve a crate name to its vendored root (`vendor/<dir>/src/lib.rs`) among already-loaded
1109
- // sources, or null. `use` names underscore but the vendor dir may hyphenate, so try both.
791
+ // `use` names underscore but the vendor dir may hyphenate, so try both.
1110
792
  export function resolveVendoredCrate(crateName, { knownSources } = {}) {
1111
793
  const norm = normName(crateName)
1112
- for (const dir of norm.includes('_') ? [norm, norm.replaceAll('_', '-')] : [norm]) {
1113
- const lib = `${VENDOR_DIR}/${dir}/src/lib.rs`
1114
- if (knownSources?.has(lib)) return lib
1115
- }
1116
- return null
794
+ return [norm, norm.replaceAll('_', '-')].map((dir) => `${VENDOR_DIR}/${dir}/src/lib.rs`).find((lib) => knownSources?.has(lib)) ?? null
1117
795
  }
1118
796
 
1119
- // --- Module trees -----------------------------------------------------------------------
1120
-
1121
- // The files walked as crate roots: every file named main.rs/lib.rs (a vendored crate's root, the
1122
- // lib beside a bin, …), every file a manifest names as its lib target (`[lib] path =
1123
- // "src/other.rs"`, given a Cargo context), then every explicit root (entry) that was loaded. Roots
1124
- // by name/manifest come first so their trees claim the files they reach (buildModuleTrees) before
1125
- // a glob-listed module file, an entry only by role, claims itself.
797
+ // Roots by name or manifest come first, so their trees claim files before a glob-listed module file claims itself.
1126
798
  export function crateRoots(sources, explicit = [], cargo = null) {
1127
- const roots = new Set()
1128
- for (const path of sources.keys()) {
1129
- if (isNamedRoot(path) || cargo?.isLibRoot(path) === true) roots.add(path)
1130
- }
1131
- for (const r of explicit) if (sources.has(r)) roots.add(r)
1132
- return roots
1133
- }
1134
-
1135
- // One module tree per crate root -- `trees`: root file → Map<modulePath, file> -- built by
1136
- // following `mod` edges breadth-first from `crate`, plus `files`: file → `{ root, modulePath }`
1137
- // (first root to reach a file claims it). A file is reached under one module path per `mod` spec
1138
- // (`mod a::b` for a declaration inside inline module `a`); a cfg-variant Map target contributes
1139
- // every variant under the same path, the first naming the module's file in the tree.
1140
- // `inlineModules` (file → the paths of its inline `mod x { … }` blocks, from scanRustItems) puts
1141
- // those modules in the tree too, owned by the file holding them -- unless a `mod` of the same
1142
- // name names a file (`#[cfg(unix)] mod imp;` beside `#[cfg(not(unix))] mod imp { … }`): the file
1143
- // is the more telling target, the inline body's is where the path was written anyway. Each file's
1144
- // entry also carries `leaves`, the cfg leaves (cfgLeaves) of every `mod` on the way down to it --
1145
- // the declaration's own cfg and the gate macro it sits in (`mountCfgs`: file → spec → target
1146
- // file → its declarations' `{ cfg, macro }`; a file declared under several is compiled under any
1147
- // one of them: an `any` leaf, see cfgLeaves and gateLeaves; `gatesOf`: file → the gate macros
1148
- // of its package, see gateLeaves) and, for a `mod` with several files, a `variant` leaf naming
1149
- // which (two variants of one `mod` never both apply) -- `parent`, the file that mounted it, and
1150
- // `roots`, every crate root whose tree reaches it (a file a build script and its lib share).
799
+ const knownRoot = (path) => ROOT_NAMES.has(path.slice(path.lastIndexOf('/') + 1)) || cargo?.isLibRoot(path) === true
800
+ return new Set([...[...sources.keys()].filter(knownRoot), ...explicit.filter((r) => sources.has(r))])
801
+ }
802
+
803
+ // Files are claimed by the first root to reach them.
1151
804
  export function buildModuleTrees(sources, resolutions, roots, inlineModules = null, mountCfgs = null, gatesOf = null) {
1152
805
  const trees = new Map()
1153
806
  const files = new Map()
1154
- const variantsOf = (target) => (target instanceof Map ? [...target] : [['*', target]])
1155
807
  const mountLeaves = (decls, path) => (decls === undefined ? [] : anyOf(decls.map(({ cfg, macro, via }) => [...cfgLeaves(cfg), ...gateLeaves(macro, gatesOf?.(path)), ...mountLeaves(via, path)])))
1156
- const mounted = (path, spec, sub, key, file, several, leaves) => [
1157
- ...leaves,
1158
- ...mountLeaves(mountCfgs?.get(path)?.get(spec)?.get(file), path),
1159
- ...(several ? [{ key: 'variant', site: sub, value: key.replace(/#\d+$/u, ''), neg: false }] : []),
1160
- ]
1161
808
  for (const root of roots) {
1162
809
  if (!sources.has(root)) continue
1163
810
  const tree = new Map([['crate', root]])
@@ -1166,50 +813,36 @@ export function buildModuleTrees(sources, resolutions, roots, inlineModules = nu
1166
813
  const queue = [[root, 'crate', 0, [], null]]
1167
814
  for (let qi = 0; qi < queue.length; qi++) {
1168
815
  const [path, modulePath, depth, leaves, parent] = queue[qi]
1169
- if (!files.has(path)) files.set(path, { root, modulePath, leaves, parent, roots: new Set() })
1170
- files.get(path).roots.add(root)
816
+ upsert(files, path, () => ({ root, modulePath, leaves, parent, roots: new Set() })).roots.add(root)
1171
817
  if (seen.has(path) || depth >= MAX_MODULE_DEPTH) continue
1172
818
  seen.add(path)
1173
- // `spec`'s module `sub` mounted on each file of `target` (its variants); the tree keeps the
1174
- // first.
1175
819
  const mount = (spec, sub, target) => {
1176
- const variants = variantsOf(target)
820
+ const variants = target instanceof Map ? [...target] : [['*', target]]
1177
821
  for (const [key, file] of variants) {
1178
822
  if (!tree.has(sub)) tree.set(sub, file)
1179
- queue.push([file, sub, depth + 1, mounted(path, spec, sub, key, file, variants.length > 1, leaves), path])
823
+ const variant = variants.length > 1 ? [{ key: 'variant', site: sub, value: key.replace(/#\d+$/u, ''), neg: false }] : []
824
+ queue.push([file, sub, depth + 1, [...leaves, ...mountLeaves(mountCfgs?.get(path)?.get(spec)?.get(file), path), ...variant], path])
1180
825
  }
1181
826
  }
1182
- // Each prefix of `segments` (`a`, `a::b`, …) below this module: an inline module whose body
1183
- // lives in this file.
827
+ // Each prefix of `segments` (`a`, `a::b`, …): an inline module whose body lives in this file.
1184
828
  const claimInline = (segments) => {
1185
829
  for (let k = 1; k <= segments.length; k++) {
1186
830
  const key = `${modulePath}::${segments.slice(0, k).join('::')}`
1187
831
  if (!tree.has(key)) tree.set(key, path)
1188
832
  }
1189
833
  }
1190
- // Module files first, then the inline modules that hold nested declarations, then the other
1191
- // inline modules: a `mod imp;` file beats a `mod imp { … }` of the same name however the
1192
- // declarations are ordered.
834
+ // Module files first, so a `mod imp;` file beats a `mod imp { … }` of the same name in any order.
1193
835
  const specs = [...(resolutions.get(path) ?? [])]
1194
836
  for (const [spec, target] of specs) {
1195
- if (!spec.startsWith('mod ')) continue
1196
- const parts = spec.slice(4).split('::')
1197
- if (parts.length > 1) continue
1198
- mount(spec, `${modulePath}::${parts[0]}`, target)
837
+ if (spec.startsWith('mod ') && !spec.includes('::')) mount(spec, `${modulePath}::${spec.slice(4)}`, target)
1199
838
  }
1200
839
  for (const [spec, target] of specs) {
1201
- // An `include!`d file's tokens are this module's: it is walked under the same module path.
1202
- if (spec.startsWith('include ')) {
1203
- queue.push([target, modulePath, depth + 1, leaves, path])
1204
- continue
840
+ // An `include!`d file's tokens are this module's.
841
+ if (spec.startsWith('include ')) queue.push([target, modulePath, depth + 1, leaves, path])
842
+ else if (spec.startsWith('mod ') && spec.includes('::')) {
843
+ claimInline(spec.slice(4).split('::').slice(0, -1))
844
+ mount(spec, `${modulePath}::${spec.slice(4)}`, target)
1205
845
  }
1206
- if (!spec.startsWith('mod ')) continue
1207
- const parts = spec.slice(4).split('::')
1208
- if (parts.length === 1) continue
1209
- // `mod a::b::name`: `a` and `a::b` are inline modules whose bodies live in this file, so
1210
- // paths into them (`a::b::x`, `super::` from `name`) resolve to it.
1211
- claimInline(parts.slice(0, -1))
1212
- mount(spec, `${modulePath}::${spec.slice(4)}`, target)
1213
846
  }
1214
847
  for (const inline of inlineModules?.get(path) ?? []) claimInline(inline)
1215
848
  }
@@ -1217,36 +850,15 @@ export function buildModuleTrees(sources, resolutions, roots, inlineModules = nu
1217
850
  return { trees, files }
1218
851
  }
1219
852
 
1220
- // The crate a path's lead may name: null for a path keyword or a sysroot crate, and -- unless the
1221
- // path is absolute (`::name::…`) -- for a name the file imported (`bindings`: `use std::io;
1222
- // io::stdin()` names no crate `io`).
853
+ // Unless the path is absolute, a name the file imported is no crate: `use std::io; io::stdin()`.
1223
854
  const crateLead = ({ segments, absolute }, bindings) => {
1224
855
  const head = segments[0]
1225
856
  return head === undefined || NON_CRATE_LEADS.has(head) || (!absolute && bindings.has(head)) ? null : head
1226
857
  }
1227
858
 
1228
- // --- Path resolution ------------------------------------------------------------------------
1229
- //
1230
- // A path is resolved against its crate's module tree, module by module. Where a segment names no
1231
- // module of the one reached, the imports written in that module may provide the name: a `use`
1232
- // binding it outright, or a glob (`pub use inner::*`) when the module the glob names -- one of
1233
- // this crate -- provides it in turn. What a name leads to is a property of (module, name) and of
1234
- // how much of the module's imports the asking module may see, so it is computed once per crate
1235
- // and shared by every path resolved (`ctx.provided`): resolution stays linear in the number of
1236
- // paths, whatever the fan-out of the crate's re-exports.
1237
- //
1238
- // Results: `{ kind: 'module', modulePath, file }` for a module of the crate; `{ kind: 'item',
1239
- // file }` for anything else the crate's files hold (an item of the module reached, a name a
1240
- // `use` brought in from the sysroot -- as far as the bundle is concerned it lives where the `use`
1241
- // is); `{ kind: 'crate', name, file }` for another in-tree crate; null when nothing here answers.
1242
- // A result that went through an import at the top level carries `via: { modulePath, consumed,
1243
- // through }`: the module whose import was followed first, how many of the path's own segments
1244
- // named modules up to it, and the file holding that import.
1245
-
1246
- // The module a visibility (scanRustItems' `vis`) written in module `modulePath` reaches: the
1247
- // crate root for `pub` / `pub(crate)`, the module itself for a private item or `pub(self)`, its
1248
- // parent for `pub(super)`, the ancestor a `pub(in …)` names (`crate::a`, `super::…`, `self`) --
1249
- // when it is one; rustc rejects any other, so that reads as `crate`.
859
+ // What a module's imports provide is computed once per (module, name) for every path: resolution is linear in paths.
860
+
861
+ // A `pub(in …)` naming no ancestor (rustc rejects it) reads as `crate`.
1250
862
  function visibilityScope(vis, modulePath) {
1251
863
  if (vis === null || vis === 'self') return modulePath
1252
864
  if (vis === 'crate') return 'crate'
@@ -1259,35 +871,22 @@ function visibilityScope(vis, modulePath) {
1259
871
  else if (segments[0] === 'super') cur = parent.split('::')
1260
872
  else return 'crate'
1261
873
  let i = 1
1262
- while (segments[i] === 'super' && cur.length > 1) {
1263
- cur.pop()
1264
- i++
1265
- }
874
+ for (; segments[i] === 'super' && cur.length > 1; i++) cur.pop()
1266
875
  const scope = [...cur, ...segments.slice(i)].join('::')
1267
876
  return modulePath === scope || modulePath.startsWith(`${scope}::`) ? scope : 'crate'
1268
877
  }
1269
878
 
1270
- // --- cfg compatibility -----------------------------------------------------------------------
1271
- //
1272
- // Which of a module's several files, imports or glob sources a path written under some cfgs can
1273
- // mean: libc's `crate::c_int` from a file under `#[cfg(unix)] mod unix;` is not the one
1274
- // `#[cfg(target_os = "fuchsia")] pub use fuchsia::*;` brings in, mio's `sys::Waker` from a file in
1275
- // one `cfg_if!` branch is not the `pub use` in another. Each file carries the leaves of the cfgs
1276
- // it was mounted under (buildModuleTrees), each import and item those of its own cfg on top; two
1277
- // leaf sets that can't hold together are exclusive, and such a candidate is skipped.
1278
-
1279
- // The conjunctive leaves of a cfg predicate, `{ key, value, neg }`: `all(unix, not(feature =
1280
- // "std"))` → `unix` and `!feature="std"`; `not(any(a, b))` → `!a`, `!b`. An `any(…)` of
1281
- // alternatives that each decide something is one leaf, `{ key: 'any', alts }`, holding when any
1282
- // alternative (a conjunction of leaves) does -- `any(target_os = "linux", target_os = "l4re")`
1283
- // can't hold along with `target_os = "aix"` -- as is a negated `all(…)`; one with an alternative
1284
- // that decides nothing (`any(unix, libc_core_cvoid)`) decides nothing itself.
879
+ // cfg compatibility: each file carries the cfg leaves it was mounted under, each import and item its own on
880
+ // top; a candidate whose leaves can't hold with the asker's is skipped (libc's per-platform `pub use fuchsia::*`).
881
+
882
+ // The conjunctive leaves `{ key, value, neg }` of a predicate. An `any(…)` (or a negated `all(…)`) is one leaf
883
+ // `{ key: 'any', alts }`, unless an alternative decides nothing (`any(unix, libc_core_cvoid)`): then nothing.
1285
884
  function cfgLeaves(pred) {
1286
- if (pred === null || pred === undefined) return []
885
+ if (pred == null) return []
1287
886
  const p = pred.trim()
1288
887
  const m = /^(all|any|not)\s*\(([\s\S]*)\)$/u.exec(p)
1289
888
  if (!m) {
1290
- if (p === 'true') return [] // holds: decides nothing (`false` is a leaf the build rules out, see leafFalse)
889
+ if (p === 'true') return [] // `false` is a leaf the build rules out (leafFalse)
1291
890
  const kv = /^((?:r#)?[\w-]+)\s*(?:=\s*"([^"]*)")?$/u.exec(p)
1292
891
  return kv ? [{ key: cfgName(kv[1]), value: kv[2] ?? null, neg: false }] : []
1293
892
  }
@@ -1296,12 +895,8 @@ function cfgLeaves(pred) {
1296
895
  if (m[1] === 'any') return anyOf(parts)
1297
896
  return parts.length === 1 ? negated(parts[0]) : []
1298
897
  }
1299
- // The leaves holding when any of `alts` (conjunctions) does: the one alternative when there is
1300
- // one (the same twice counts once, and one holding whenever another does adds nothing: `a ∨ (a ∧
1301
- // b)` is `a`), an `any` leaf of them, nothing when one decides nothing -- or, for more than
1302
- // MAX_ALTERNATIVES (a module reached along every path of a dense glob cycle, a cfg listing a
1303
- // score of platforms) or one undecided already, the undecided leaf: too many to tell apart along
1304
- // every path, and never to be taken for holding (see UNDECIDED_KEY).
898
+ // The leaves holding when any of `alts` does, once duplicates and implied ones go (`a ∨ (a ∧ b)` is `a`); past
899
+ // MAX_ALTERNATIVES or over an undecided one, the undecided leaf.
1305
900
  const MAX_ALTERNATIVES = 16
1306
901
  const anyOf = (alts) => {
1307
902
  const uniq = new Map()
@@ -1318,15 +913,9 @@ const anyOf = (alts) => {
1318
913
  if (list.length <= 1) return list[0] ?? []
1319
914
  return [{ key: 'any', value: null, neg: false, alts: list }]
1320
915
  }
1321
- // The key of the leaf an `any` the loader doesn't follow is kept as (`many`: its alternatives,
1322
- // for its cfg text, see leafText): one no build decides (leafFalse), no asker holds -- itself
1323
- // under one included (holds) -- that is no custom cfg (doubtful) and never exclusive with
1324
- // anything (prepared), its negation too. So a candidate under it is only maybe the answer, beside
1325
- // the others (withAlternatives), and never certain; and an `any` with one among its alternatives
1326
- // is undecided too, which keeps the sets along a dense glob cycle few.
916
+ // An `any` the loader doesn't follow (`many`: its alternatives): no build decides it, no asker holds it, it is
917
+ // never custom nor exclusive, so a candidate under it is only maybe the answer, never certain.
1327
918
  const UNDECIDED_KEY = 'any(…)!'
1328
- // The leaves of a conjunction's negation: one leaf flips, an `any` leaf's negation is every
1329
- // alternative's, several leaves negate to any of their negations.
1330
919
  const negated = (leaves) => {
1331
920
  if (leaves.length === 0) return []
1332
921
  if (leaves.length > 1) return anyOf(leaves.map((l) => negated([l])))
@@ -1334,20 +923,14 @@ const negated = (leaves) => {
1334
923
  return l.alts === undefined ? [{ ...l, neg: !l.neg }] : l.alts.flatMap(negated)
1335
924
  }
1336
925
 
1337
- // A cfg gate macro: `cfg_<x>!` (tokio's `cfg_io_uring! { … }`, mio's `cfg_os_poll! { … }`)
1338
- // wraps each item of its body in the cfg its definition writes -- `($($item:item)*) => { $(
1339
- // #[cfg(feature = "rt")] $item )* }` -- read from that definition (gatePredicate); `cfg_if!` is
1340
- // read branch by branch, any other macro's body is no gate.
926
+ // A gate macro (tokio's `cfg_io_uring!`) wraps each item of its body in the cfg its definition writes.
1341
927
  const GATE_MACRO_RE = /^cfg_(?!if$)\w+$/u
1342
- // The cfg a gate macro's definition puts on what it wraps: the `#[cfg(…)]` attributes of its
1343
- // transcribers (their conjunction), when every arm writes the same ones; null when an arm writes
1344
- // none or the arms differ -- a gate the loader can't see into. `code` and `masked` are the lexed
1345
- // views, `open`/`close` the braces of the `macro_rules!` body.
928
+ // The conjunction of the transcribers' `#[cfg(…)]`s when every arm writes the same; else null.
1346
929
  function gatePredicate(code, masked, open, close) {
1347
930
  let seen = null
1348
931
  for (let i = masked.indexOf('=>', open); i !== -1 && i < close; i = masked.indexOf('=>', i + 2)) {
1349
932
  let start = i + 2
1350
- while (/\s/u.test(masked[start] ?? '')) start++
933
+ while (WS_RE.test(masked[start] ?? '')) start++
1351
934
  if (!'{(['.includes(masked[start] ?? ' ')) continue
1352
935
  const end = matchClose(masked, start)
1353
936
  const preds = []
@@ -1363,46 +946,28 @@ function gatePredicate(code, masked, open, close) {
1363
946
  }
1364
947
  return seen
1365
948
  }
1366
- // The leaves of what a gate macro's body declares: its definition's cfg (`gates`: macro name →
1367
- // predicate, null when unreadable, see gatePredicate), else a leaf of its own name -- which holds
1368
- // along with anything and is never certain: `cfg_x!` and `cfg_not_x!` are not taken for each
1369
- // other's negation by their names alone.
949
+ // An unreadable definition gives a never-certain leaf of the macro's name: `cfg_not_x!` isn't `not(cfg_x!)`.
1370
950
  function gateLeaves(macro, gates = null) {
1371
- if (macro === null || macro === undefined || !GATE_MACRO_RE.test(macro)) return []
951
+ if (macro == null || !GATE_MACRO_RE.test(macro)) return []
1372
952
  const pred = gates?.get(macro)
1373
953
  return typeof pred === 'string' ? cfgLeaves(pred) : [{ key: `${macro}!`, value: null, neg: false }]
1374
954
  }
1375
955
 
1376
- // Keys a target has one value of: `target_os = "linux"` and `target_os = "macos"` never both hold
1377
- // (`feature`, `target_family` and `target_has_atomic` may take several).
1378
956
  const SINGLE_VALUED_CFG_KEYS = new Set(['target_os', 'target_arch', 'target_env', 'target_vendor', 'target_abi', 'target_pointer_width', 'target_endian', 'panic'])
1379
- // Operating systems whose targets are not `unix`.
1380
957
  const NON_UNIX_OS = new Set(['windows', 'none', 'uefi', 'wasi', 'solana', 'cuda', 'xous', 'zkvm', 'psp', 'unknown'])
1381
958
  const isWindowsLeaf = (l) => (l.key === 'windows' && l.value === null) || ((l.key === 'target_os' || l.key === 'target_family') && l.value === 'windows')
1382
959
  const isUnixLeaf = (l) => (l.key === 'unix' && l.value === null) || (l.key === 'target_family' && l.value === 'unix')
1383
960
 
1384
- // Whether two leaf lists (conjunctions) can't hold at once: a leaf and its negation; two values of
1385
- // a single-valued key; two variants of one `mod` (`variant` leaves, see buildModuleTrees); Windows
1386
- // against unix; an `any` leaf none of whose alternatives can hold with the other. Asked once per
1387
- // pair of leaf sets (compatible), over what `prepared` gathers of each list.
1388
961
  function cfgExclusive(a, b) {
1389
- const pa = prepared(a)
1390
- const pb = prepared(b)
962
+ const [pa, pb] = [prepared(a), prepared(b)]
1391
963
  for (const k of pa.negKeys) if (pb.keys.has(k)) return true
1392
- for (const [k, v] of pa.values) {
1393
- const w = pb.values.get(k)
1394
- if (w !== undefined && w !== v) return true
1395
- }
964
+ for (const [k, v] of pa.values) if (pb.values.has(k) && pb.values.get(k) !== v) return true
1396
965
  if ((pa.windows && (pb.unix || pb.otherOs)) || (pb.windows && (pa.unix || pa.otherOs)) || (pa.unix && pb.nonUnixOs) || (pb.unix && pa.nonUnixOs)) return true
1397
966
  for (const x of pa.anys) if (x.alts.every((alt) => cfgExclusive(alt, b))) return true
1398
967
  for (const y of pb.anys) if (y.alts.every((alt) => cfgExclusive(a, alt))) return true
1399
968
  return false
1400
969
  }
1401
- // What cfgExclusive asks of a leaf list, gathered once and kept on it: its plain leaves' keys and
1402
- // their negations', the value each positive single-valued key (and `mod` variant site) takes --
1403
- // `MANY` for several, which nothing holds along with -- its Windows / unix standing (a
1404
- // `target_os` other than windows, `otherOs`; one of the non-unix ones, `nonUnixOs`) and its `any`
1405
- // leaves.
970
+ // What cfgExclusive asks of a leaf list, kept on it; a key with several values (`MANY`) holds with nothing.
1406
971
  const MANY = Symbol('many')
1407
972
  function prepared(leaves) {
1408
973
  if (leaves.prep !== undefined) return leaves.prep
@@ -1423,41 +988,31 @@ function prepared(leaves) {
1423
988
  }
1424
989
  if (isWindowsLeaf(l)) prep.windows = true
1425
990
  if (isUnixLeaf(l)) prep.unix = true
1426
- if (l.key === 'target_os' && l.value !== null) {
1427
- if (l.value !== 'windows') prep.otherOs = true
1428
- if (NON_UNIX_OS.has(l.value)) prep.nonUnixOs = true
1429
- }
991
+ if (l.key === 'target_os' && l.value !== null && l.value !== 'windows') prep.otherOs = true
992
+ if (l.key === 'target_os' && NON_UNIX_OS.has(l.value)) prep.nonUnixOs = true
1430
993
  }
1431
994
  leaves.prep = prep
1432
995
  return prep
1433
996
  }
1434
997
 
1435
- // A stable text for one leaf; an `any` leaf's names its alternatives. Kept per leaf: an `any`
1436
- // leaf's is asked for again and again as sets of them are interned and compared.
998
+ // Kept per leaf: an `any` leaf's is asked for again and again as sets of them are interned and compared.
1437
999
  const leafKeys = new WeakMap()
1438
- const leafKey = (l) => cached(leafKeys, l, () => (l.alts === undefined ? `${l.neg ? '!' : ''}${l.key}${l.site === undefined ? '' : `@${l.site}`}${l.value === null ? '' : `=${l.value}`}` : `any(${l.alts.map((alt) => alt.map(leafKey).toSorted().join('&')).toSorted().join(',')})`))
1000
+ const leafKey = (l) => upsert(leafKeys, l, () => (l.alts === undefined ? `${l.neg ? '!' : ''}${l.key}${l.site === undefined ? '' : `@${l.site}`}${l.value === null ? '' : `=${l.value}`}` : `any(${l.alts.map((alt) => alt.map(leafKey).toSorted().join('&')).toSorted().join(',')})`))
1439
1001
 
1440
- // Leaf sets are interned per tree (`sets`: key → `{ leaves, key }`), one object per distinct set:
1441
- // the union of the cfgs along a chain of globs stays small however long the chain, and the
1442
- // compatibility verdict for a pair of sets is looked up by key, not computed again.
1002
+ // Interned per tree: a chain of globs' union stays small, and the verdict for a pair of sets is looked up by key.
1443
1003
  const NO_LEAVES = { leaves: [], key: '' }
1444
1004
  function leafSet(leaves, sets) {
1445
1005
  if (leaves.length === 0) return NO_LEAVES
1446
- const uniq = new Map()
1447
- for (const l of leaves) uniq.set(leafKey(l), l)
1006
+ const uniq = new Map(leaves.map((l) => [leafKey(l), l]))
1448
1007
  const key = [...uniq.keys()].toSorted().join('|')
1449
- return sets.get(key) ?? sets.set(key, { leaves: [...uniq.values()], key }).get(key)
1008
+ return upsert(sets, key, () => ({ leaves: [...uniq.values()], key }))
1450
1009
  }
1451
- // A union is asked for again and again along a closure (a glob's leaves with each candidate's): kept
1452
- // on the first set, by the second's key.
1010
+ // Asked for again and again along a closure: kept on the first set, by the second's key.
1453
1011
  const unionLeaves = (a, b, sets) => {
1454
1012
  if (a.key === '') return b
1455
1013
  if (b.key === '' || b.key === a.key) return a
1456
- const unions = (a.unions ??= new Map())
1457
- return unions.get(b.key) ?? unions.set(b.key, leafSet([...a.leaves, ...b.leaves], sets)).get(b.key)
1014
+ return upsert((a.unions ??= new Map()), b.key, () => leafSet([...a.leaves, ...b.leaves], sets))
1458
1015
  }
1459
- // The leaves holding when either of two sets does (a module two glob paths reach): an `any` of
1460
- // the two -- of their alternatives, when one is an `any` leaf already.
1461
1016
  const eitherLeaves = (a, b, sets) => {
1462
1017
  if (a.key === b.key) return a
1463
1018
  if (a.key === '' || b.key === '') return NO_LEAVES
@@ -1465,119 +1020,55 @@ const eitherLeaves = (a, b, sets) => {
1465
1020
  return leafSet(anyOf([...altsOf(a), ...altsOf(b)]), sets)
1466
1021
  }
1467
1022
 
1468
- // The leaf set of an `asker` -- `{ file, set, keys, compat, sure, doubt }`, the file a path was
1469
- // written in, its set the leaves it was mounted under plus, when the build's target is known, the
1470
- // target's cfgs (`unix`, `target_os = "linux"`, …: what every file of the build is compiled
1471
- // under) -- against a candidate's: exclusive candidates are skipped; no asker (an internal query)
1472
- // skips nothing. The verdict is kept per pair of sets in `compat`, shared by the askers of one set.
1473
- const compatible = (asker, set) => asker === undefined || set.key === '' || asker.contradictory || cached(asker.compat, set.key, () => !cfgExclusive(asker.set.leaves, set.leaves))
1474
- // Whether a leaf list can't hold in the asker's build (scanRustItems' build, see buildOf): one of
1475
- // its leaves the build decides is false -- a feature that is off, a platform the build isn't --
1476
- // or an `any` leaf each of whose alternatives is. What a gate macro's cfg wraps (tokio's
1477
- // `cfg_rt! { … }` with `rt` off) is then taken last (`dead`, see pick): after any candidate the
1478
- // build may compile, and only when there is none -- a path written behind the same gate, whose
1479
- // code the build doesn't compile either, means what it would mean where it compiles. For an
1480
- // asker whose own leaves can't hold (`deadHere`) the build says nothing.
1023
+ // No asker (an internal query) skips nothing; verdicts are kept per pair of sets, shared by one set's askers.
1024
+ const compatible = (asker, set) => asker === undefined || set.key === '' || asker.contradictory || upsert(asker.compat, set.key, () => !cfgExclusive(asker.set.leaves, set.leaves))
1025
+ // A candidate the asker's build can't compile is taken last, only when nothing the build compiles answers; an
1026
+ // asker whose own leaves can't hold (`deadHere`) gets no verdict.
1481
1027
  const leafFalse = (l, build) => {
1482
1028
  if (l.alts !== undefined) return l.alts.every((alt) => alt.some((m) => leafFalse(m, build)))
1483
- if (l.key === 'false' && l.value === null) return !l.neg // the literal (cfgLeaves)
1029
+ if (l.key === 'false' && l.value === null) return !l.neg
1484
1030
  const verdict = evalCfgKey(l.key, l.value, build)
1485
1031
  return l.neg ? verdict === true : verdict === false
1486
1032
  }
1487
1033
  const deadUnder = (leaves, build) => build !== undefined && leaves.some((l) => leafFalse(l, build))
1488
- const deadFor = (asker, set) => asker !== undefined && !asker.deadHere && set.key !== '' && cached(asker.dead, set.key, () => deadUnder(set.leaves, asker.build))
1489
- // Whether every leaf of a candidate's set is among the asker's own (`keys`, by leafKey): the two
1490
- // are certainly compiled together, not merely possibly (compatible). libc's aix/mod.rs, mounted
1491
- // under `target_os = "aix"`, asks `crate::fsid_t`: the aix glob's, under that same leaf, is the
1492
- // answer, rather than the first platform's whose cfg is an `any(…)` nothing rules out. Kept per
1493
- // pair in `sure`. An `any` leaf holds when one of its alternatives does, and so does the negation
1494
- // of a custom cfg (`not(loom)`: presumably off, see doubtful). An asker under no cfg holds nothing
1495
- // else: for it, candidates under cfgs of their own are each only maybe the answer, and all of
1496
- // them are (see withAlternatives), not the first in written order.
1034
+ const deadFor = (asker, set) => asker !== undefined && !asker.deadHere && set.key !== '' && upsert(asker.dead, set.key, () => deadUnder(set.leaves, asker.build))
1035
+ // Whether a candidate's leaves are all the asker's own: certainly compiled together (libc's aix/mod.rs asking
1036
+ // `crate::fsid_t` takes the aix glob's). A custom cfg's negation (`not(loom)`) is presumed to hold.
1497
1037
  const holds = (l, keys, custom) => l.many === undefined && (keys.has(leafKey(l)) || (l.alts === undefined ? l.neg && custom(l.key) : l.alts.some((alt) => alt.every((m) => holds(m, keys, custom)))))
1498
- const entailed = (asker, set) => asker === undefined || set.key === '' || (!asker.contradictory && cached(asker.sure, set.key, () => set.leaves.every((l) => holds(l, asker.keys, asker.custom))))
1499
- // The cfgs rustc and cargo set: anything else in a positive leaf is a custom `--cfg` (`loom`,
1500
- // `docsrs`, `tokio_unstable`, mio's `mio_unsupported_force_poll_poll`), off in a default build
1501
- // unless a build script or the rustflags set it -- so a candidate under one is `doubtful`: taken
1502
- // after a compatible candidate under none (never ruled out: the loader doesn't know the build's
1503
- // flags). One the asker's package may set (its build script prints `cargo:rustc-cfg=<name>`, a
1504
- // rustflags `--cfg` names it: cargo.js cfgsSetFor) is no custom cfg for it: neither doubtful nor
1505
- // presumed off; nor is any when its build script may print one the loader can't read (`asker.
1506
- // custom`, see customFor). An `any` leaf is doubtful when each alternative is, or can't hold with
1507
- // the asker. A gate macro's leaf (`cfg_x!`) and a `mod` variant's are neither, nor is a leaf the
1508
- // asker holds itself (a file under `#[cfg(loom)]` takes the loom candidates).
1038
+ const entailed = (asker, set) => asker === undefined || set.key === '' || (!asker.contradictory && upsert(asker.sure, set.key, () => set.leaves.every((l) => holds(l, asker.keys, asker.custom))))
1039
+ // The cfgs rustc and cargo set; any other positive leaf is a custom `--cfg` (`loom`, `docsrs`), off by default,
1040
+ // so a candidate under one is `doubtful`, unless the asker's package may set it (cargo.js cfgsSetFor).
1509
1041
  const KNOWN_CFG_KEYS = new Set([...TARGET_CFG_KEYS, 'false', 'feature', 'test', 'doctest', 'doc', 'debug_assertions', 'overflow_checks', 'panic', 'proc_macro', 'miri', 'sanitize', 'target_feature', 'target_thread_local', 'ub_checks', 'relocation_model', 'fmt_debug', 'clippy', 'rustfmt', 'variant'])
1510
1042
  const customKey = (key) => !KNOWN_CFG_KEYS.has(key) && !key.endsWith('!')
1511
- // The custom-cfg test of an asker whose build may set `settable` (`{ names, any }`), one per object.
1512
1043
  const customMemo = new WeakMap()
1513
1044
  const NO_CUSTOM = () => false
1514
1045
  const customFor = (settable) => {
1515
- if (settable === undefined || settable === null) return customKey
1046
+ if (settable == null) return customKey
1516
1047
  if (settable.any) return NO_CUSTOM
1517
1048
  if (settable.names.size === 0) return customKey
1518
- if (!customMemo.has(settable)) customMemo.set(settable, (key) => customKey(key) && !settable.names.has(key))
1519
- return customMemo.get(settable)
1049
+ return upsert(customMemo, settable, () => (key) => customKey(key) && !settable.names.has(key))
1520
1050
  }
1521
1051
  const doubtfulLeaf = (l, askerLeaves, custom) => (l.alts === undefined ? !l.neg && custom(l.key) : l.alts.every((alt) => cfgExclusive(alt, askerLeaves) || alt.some((m) => doubtfulLeaf(m, askerLeaves, custom))))
1522
- // Whether a leaf list has any custom cfg at all -- else no asker finds it doubtful (kept on the
1523
- // set for the plain test, which most askers share).
1052
+ // A set with no custom cfg at all is doubtful to no asker.
1524
1053
  const hasCustom = (leaves, custom) => leaves.some((l) => (l.alts === undefined ? !l.neg && custom(l.key) : l.alts.some((alt) => hasCustom(alt, custom))))
1525
1054
  const doubtful = (asker, set) => {
1526
1055
  if (asker === undefined || set.key === '') return false
1527
- if (set.custom === undefined) set.custom = hasCustom(set.leaves, customKey)
1528
- return set.custom && cached(asker.doubt, set.key, () => set.leaves.some((l) => !holds(l, asker.keys, asker.custom) && doubtfulLeaf(l, asker.set.leaves, asker.custom)))
1056
+ set.custom ??= hasCustom(set.leaves, customKey)
1057
+ return set.custom && upsert(asker.doubt, set.key, () => set.leaves.some((l) => !holds(l, asker.keys, asker.custom) && doubtfulLeaf(l, asker.set.leaves, asker.custom)))
1529
1058
  }
1530
1059
 
1531
- // --- memoization -----------------------------------------------------------------------------
1532
-
1533
- // Memoization with cycle detection (globClosure; the same bookkeeping serves globSource, pick and
1534
- // the candidate lists). A query asked again while it is being computed -- an import cycle: a
1535
- // glob into a module whose own globs lead back -- reads as `empty`, and the entry in progress is
1536
- // marked hit (`ctx.hits`, by its depth on the stack of computations under way, `ctx.walking`).
1537
- // Whatever is computed on top of such a read may be short and is not kept, so a later query from
1538
- // elsewhere in the cycle computes it afresh. The entry that was hit is kept once complete, but
1539
- // may be short itself (it read nothing for its own name) -- it is marked `cyclic`, with its
1540
- // `compute`, for stabilizeClosures to run again once every entry is in place.
1541
- function memoized(memo, key, ctx, compute, { empty }) {
1542
- const hit = memo.get(key)
1543
- if (hit !== undefined) {
1544
- if (hit.depth !== undefined) {
1545
- ctx.hits.add(hit.depth)
1546
- return empty
1547
- }
1548
- return hit.value
1549
- }
1550
- const depth = ctx.walking++
1551
- memo.set(key, { depth })
1552
- const value = compute()
1553
- const cyclic = ctx.hits.delete(depth)
1554
- ctx.walking--
1555
- if (minHit(ctx) < depth) memo.delete(key)
1556
- else memo.set(key, cyclic ? { value, cyclic, compute } : { value })
1557
- return value
1558
- }
1559
-
1560
- // Recompute the glob closures that were computed short of a cycle (memoized), each against the
1561
- // others as they now stand, until none grows: with every entry complete, a closure is the union
1562
- // of its globs' targets and their settled closures, whatever the order the queries came in. The
1563
- // lookups made meanwhile (glob sources, imports' answers, candidate lists) are dropped first, as
1564
- // they may rest on the short closures. True when anything changed -- the paths resolved so far must be resolved
1565
- // again.
1060
+ // Recompute the glob closures computed short of an import cycle against each other until none grows, dropping
1061
+ // first the lookups that may rest on the short ones. True when the paths resolved so far must be resolved again.
1566
1062
  function stabilizeClosures(ctx) {
1567
- const cyclic = []
1568
- for (const memo of ctx.closures.values()) for (const entry of memo.values()) if (entry.cyclic === true) cyclic.push(entry)
1063
+ const cyclic = [...ctx.closures.values()].flatMap((memo) => [...memo.values()].filter((entry) => entry.cyclic === true))
1569
1064
  if (cyclic.length === 0) return false
1570
1065
  const reset = () => {
1571
1066
  ctx.provided.clear()
1572
1067
  ctx.opaque.clear()
1573
1068
  ctx.opaqueGlobs.clear()
1574
- for (const byModule of ctx.imports.values()) {
1575
- for (const of of byModule.values()) {
1576
- for (const im of [...of.globs, ...[...of.named.values()].flat()]) {
1577
- delete im.source
1578
- delete im.answers
1579
- }
1580
- }
1069
+ for (const im of [...ctx.imports.values()].flatMap((byModule) => [...byModule.values()].flatMap(importsIn))) {
1070
+ delete im.source
1071
+ delete im.answers
1581
1072
  }
1582
1073
  }
1583
1074
  let changed = false
@@ -1599,70 +1090,31 @@ function stabilizeClosures(ctx) {
1599
1090
  }
1600
1091
  const MAX_STABILIZE_ROUNDS = 8
1601
1092
 
1093
+ const importsIn = (of) => [...of.globs, ...[...of.named.values()].flat()]
1094
+
1602
1095
  // The shallowest computation under way that was cycled back to, Infinity for none.
1603
- function minHit(ctx) {
1604
- let min = Infinity
1605
- for (const d of ctx.hits) if (d < min) min = d
1606
- return min
1607
- }
1096
+ const minHit = (ctx) => Math.min(...ctx.hits)
1608
1097
 
1609
- // Number of leading module-path segments `a` and `b` share (`crate::a::b` / `crate::a::c` → 2).
1610
1098
  function commonDepth(a, b) {
1611
- const n = Math.min(a.length, b.length)
1612
- let i = 0
1099
+ const [x, y] = [a.split('::'), b.split('::')]
1613
1100
  let depth = 0
1614
- while (i < n && a[i] === b[i]) {
1615
- if (a[i] === ':' && a[i + 1] === ':') {
1616
- depth++
1617
- i += 2
1618
- } else {
1619
- i++
1620
- }
1621
- }
1622
- // The segment the match stopped in counts when it is whole in both: each ends there or goes on
1623
- // with a `::`.
1624
- const wholeA = i === a.length || a.startsWith('::', i)
1625
- const wholeB = i === b.length || b.startsWith('::', i)
1626
- return wholeA && wholeB ? depth + 1 : depth
1101
+ while (depth < x.length && depth < y.length && x[depth] === y[depth]) depth++
1102
+ return depth
1627
1103
  }
1628
1104
 
1629
- // What the imports written in module `at` make of `name`, as seen from module `from` (`from`
1630
- // decides which imports apply: a `pub(in scope)` one to a path written inside `scope`, a plain
1631
- // `use` to one written in `at` or below). A `use` binding the name outright is followed (bound);
1632
- // else the modules of this crate the module's globs reach (globClosure) are searched for it, each
1633
- // as seen from the module whose glob named it: as a child module, an item it defines, or through
1634
- // a `use` of its own. A glob into the sysroot or into another crate provides nothing: it can't be
1635
- // told what it brings in, and claiming everything would hide the crate's own edges. `asker` is
1636
- // the file the path was written in, with its cfg leaves (see cfg compatibility): an import, item
1637
- // or glob source exclusive with them is passed over. The candidates are listed once per (at,
1638
- // name, what `from` may see) -- providedAll -- and the asker picks among them.
1105
+ // Scopes are ancestors of `at`, so `from` sees as deep as it shares with `at`.
1639
1106
  function provided(root, at, name, from, ctx, asker, ns = null) {
1640
- return pick(providedAll(root, at, name, commonDepth(from, at), ctx), root, ctx, asker, ns) // scopes are ancestors of `at`: `from` is inside those this deep or shallower
1641
- }
1642
-
1643
- // The answer of the first of `candidates` (see providedAll: in written order) whose cfgs the
1644
- // asker's entail (entailed) -- else the first compatible with them under no custom cfg, else the
1645
- // first compatible at all (doubtful) -- in the namespace `ns` asks for: for a path's lead or
1646
- // prefix (`type`), a `fn log` can't lead `log::info`; for a macro (`macro`), an item the module
1647
- // defines (`fn m`), a module, or an import of a module or crate is no `m!`. A child module,
1648
- // item or macro answers as it is; an import answers what it leads to (followImport, on the
1649
- // asker's behalf, so its cfgs hold along the whole chain), resolved when first asked and then
1650
- // kept on the import per asker -- unless the answer came out of an import cycle still being
1651
- // resolved (see memoized). An import being followed that is met again leads back to itself (`use
1652
- // log;` in every platform file of a module asks the module for `log`, which is these imports):
1653
- // nothing here. An import that leads nowhere the bundle can see -- when it is a path, not a crate
1654
- // named outright (`use serde;`, passed over) -- answers where it is: the name is an item of its
1655
- // file, as far as the bundle knows. A value where a type is wanted (a `fn` defined here, an
1656
- // import of one) is passed over too, and when nothing else answers, the answer is VALUE_ONLY
1657
- // rather than nothing: the name is taken, but not in this namespace.
1107
+ return pick(providedAll(root, at, name, commonDepth(from, at), ctx), root, ctx, asker, ns)
1108
+ }
1109
+
1110
+ // The first of `candidates` (in written order) the asker's cfgs entail, else the compatible ones under no doubt
1111
+ // (withAlternatives), else the first doubtful, else the first dead; VALUE_ONLY for a value where a type is wanted.
1658
1112
  function pick(candidates, root, ctx, asker, ns = null) {
1659
- const maybes = [] // the compatible answers under no doubt, for when none is entailed (see withAlternatives)
1660
- let doubted = null // the first compatible answer under a doubtful cfg, for when there is nothing else
1661
- let lastResort = null // the first under a cfg the asker's build can't compile (see deadUnder)
1662
- let valueOnly = false // a candidate passed over for being a value where a type is wanted
1663
- for (let i = 0; ; i++) {
1664
- const c = candidates.get(i)
1665
- if (c === undefined) break
1113
+ const maybes = []
1114
+ let doubted = null
1115
+ let lastResort = null
1116
+ let valueOnly = false
1117
+ for (const c of candidates) {
1666
1118
  if (!compatible(asker, c.leaves)) continue
1667
1119
  if (ns === 'type' && c.ns === 'value') {
1668
1120
  valueOnly = true
@@ -1679,6 +1131,7 @@ function pick(candidates, root, ctx, asker, ns = null) {
1679
1131
  r = c.answer
1680
1132
  } else {
1681
1133
  const im = c.import
1134
+ // Met again while being followed (`use log;` asks its own module for `log`).
1682
1135
  if (im.following !== undefined) {
1683
1136
  ctx.hits.add(im.following)
1684
1137
  continue
@@ -1689,21 +1142,17 @@ function pick(candidates, root, ctx, asker, ns = null) {
1689
1142
  const depth = ctx.walking++
1690
1143
  im.following = depth
1691
1144
  r = followImport(im, root, ctx, asker, ns)
1692
- // The name a macro an import leads to is defined by: the import's (`use dep::mac as m;`).
1145
+ // A macro is defined under the import's name for it (`use dep::mac as m;`).
1693
1146
  if (ns === 'macro' && r?.kind === 'item' && r.macroName === undefined && im.segments.length > 0) r = { ...r, macroName: im.segments.at(-1) }
1694
1147
  im.following = undefined
1695
1148
  ctx.walking--
1696
1149
  ctx.hits.delete(depth)
1697
1150
  if (minHit(ctx) > depth) (im.answers ??= new Map()).set(key, r)
1698
1151
  }
1699
- // Followed as a macro to a file defining no macro of the name (`pub use util::helper;` of
1700
- // a `fn helper` beside the root's `#[macro_export] macro_rules! helper`), it binds no macro:
1701
- // what it imports lives in another namespace.
1702
- if (ns === 'macro' && r?.kind === 'item' && ctx.fileMacros.get(r.file)?.has(r.macroName) !== true) continue
1703
- // An import leading out of the bundle (`pub(crate) use std::sync::atomic::AtomicU64;`, a
1704
- // crate that isn't vendored) binds the name all the same: an item of its own file, as far
1705
- // as the bundle knows, under its cfgs. One naming a crate outright (`use serde;`) answers
1706
- // nothing: the lead is the crate's to report.
1152
+ // `pub use util::helper;` of a `fn helper` binds no macro.
1153
+ if (ns === 'macro' && r?.kind === 'item' && !definesMacro(ctx, r.file, r.macroName)) continue
1154
+ // An import leading out of the bundle binds the name all the same, as an item of its own file; one naming
1155
+ // a crate outright (`use serde;`) answers nothing: the lead is the crate's to report.
1707
1156
  if (r === null) {
1708
1157
  if (im.segments.length === 1) continue
1709
1158
  r = { kind: 'item', file: im.file }
@@ -1714,10 +1163,11 @@ function pick(candidates, root, ctx, asker, ns = null) {
1714
1163
  }
1715
1164
  if (ns === 'macro' && (r.kind === 'module' || r.kind === 'crate')) continue
1716
1165
  }
1717
- if (sure) return { ...r, through: c.through }
1718
- if (dead) lastResort = { ...r, through: c.through }
1719
- else if (doubt) doubted = { ...r, through: c.through }
1720
- else maybes.push({ answer: { ...r, through: c.through }, leaves: c.leaves })
1166
+ const answer = { ...r, through: c.through }
1167
+ if (sure) return answer
1168
+ if (dead) lastResort = answer
1169
+ else if (doubt) doubted = answer
1170
+ else maybes.push({ answer, leaves: c.leaves })
1721
1171
  }
1722
1172
  if (maybes.length > 0) return withAlternatives(maybes, asker)
1723
1173
  if (doubted !== null) return doubted
@@ -1725,39 +1175,29 @@ function pick(candidates, root, ctx, asker, ns = null) {
1725
1175
  return valueOnly ? VALUE_ONLY : null
1726
1176
  }
1727
1177
 
1728
- // A leaf list as cfg text, for a key: `all(unix, feature = "std")`, `not(loom)`, `any(…)`, a gate
1729
- // macro by name (`cfg_x!`); a `mod` variant's leaf by the key of the variant (its cfg, or its
1730
- // macro), and only when nothing else tells the candidates apart. Null for no leaves.
1731
1178
  const leafText = (l) => {
1732
1179
  if (l.alts !== undefined) return `any(${l.alts.map((alt) => cfgTextOf(alt) ?? '*').join(', ')})`
1733
1180
  const plain = l.many !== undefined ? manyText(l) : (l.value === null ? l.key : `${l.key} = "${l.value}"`)
1734
1181
  return l.neg ? `not(${plain})` : plain
1735
1182
  }
1736
- // An undecided leaf's text (see UNDECIDED_KEY): the `any(…)` of its alternatives, kept per leaf, or
1737
- // its count when that runs long (undecided leaves within undecided leaves).
1183
+ // Kept per leaf, and cut short when it runs long (undecided leaves within undecided leaves).
1738
1184
  const manyTexts = new WeakMap()
1739
- const manyText = (l) => cached(manyTexts, l, () => {
1185
+ const manyText = (l) => upsert(manyTexts, l, () => {
1740
1186
  const text = `any(${l.many.map((alt) => cfgTextOf(alt) ?? '*').join(', ')})`
1741
1187
  return text.length > 4096 ? `any(${l.many.length} alternatives)` : text
1742
1188
  })
1189
+ // A leaf list as cfg text, for a key; a `mod` variant's leaf only when nothing else tells candidates apart.
1743
1190
  function cfgTextOf(leaves) {
1744
1191
  const own = leaves.filter((l) => l.key !== 'variant')
1745
- // What a variant is, rather than all it isn't: its positive leaves (a `cfg_if!` branch's own
1746
- // cfg, not the negations of the branches before it), else its `any(…)` of positive ones.
1192
+ // What a variant is rather than all it isn't: a `cfg_if!` branch's own cfg, not the earlier branches' negations.
1747
1193
  const positive = own.filter((l) => l.alts === undefined && !l.neg)
1748
1194
  const anyPositive = own.filter((l) => l.alts !== undefined && l.alts.every((alt) => alt.every((m) => m.alts === undefined && !m.neg)))
1749
1195
  const shown = positive.length + anyPositive.length > 0 ? [...positive, ...anyPositive] : (own.length > 0 ? own : leaves)
1750
1196
  return joinCfgs([...new Set(shown.map((l) => (l.key === 'variant' ? l.value : leafText(l))))].toSorted())
1751
1197
  }
1752
1198
 
1753
- // The answer of several candidates none of which the asker's cfgs entail (pick, definedIn): each
1754
- // applies under cfgs of its own -- a file under no platform cfg asking libc's `crate::sockaddr`,
1755
- // defined per platform -- so the first in written order is no answer. The first, carrying
1756
- // `alternatives`: cfg key → file, each distinct file under the leaves of its first candidate
1757
- // that the asker doesn't hold itself. A module first -- one a path may go on through -- carries
1758
- // `branches` instead, cfg key → each distinct answer (its own included), for walkPath to follow
1759
- // the rest of the path through each (`use unix as imp;` beside `use windows as imp;`). One file
1760
- // or module: that answer alone.
1199
+ // Several candidates none of which the asker's cfgs entail (libc's per-platform `crate::sockaddr`): the first,
1200
+ // with `alternatives` (cfg key → each distinct file), or for a module, `branches` (cfg key → each answer).
1761
1201
  function withAlternatives(found, asker) {
1762
1202
  const [first] = found
1763
1203
  const idOf = (answer) => (answer.kind === 'module' ? `mod ${answer.modulePath}` : answer.file)
@@ -1768,25 +1208,14 @@ function withAlternatives(found, asker) {
1768
1208
  const id = idOf(answer)
1769
1209
  if (id === undefined || seen.has(id)) continue
1770
1210
  seen.add(id)
1771
- // Kept per asker set (the text leaves out what the asker holds itself).
1772
1211
  const textOf = () => cfgTextOf(leaves.leaves.filter((l) => asker === undefined || !asker.keys.has(leafKey(l))))
1773
- const base = cfgKey(asker === undefined ? textOf() : cached(asker.texts, leaves.key, textOf))
1774
- let key = base
1775
- for (let k = 2; keyed.has(key); k++) key = `${base}#${k}`
1776
- keyed.set(key, answer)
1212
+ keyed.set(freshKey(keyed, cfgKey(asker === undefined ? textOf() : upsert(asker.texts, leaves.key, textOf))), answer)
1777
1213
  }
1778
1214
  if (first.answer.kind === 'module') return { ...first.answer, branches: keyed }
1779
1215
  return { ...first.answer, alternatives: new Map([...keyed].filter(([, answer]) => answer.file !== undefined).map(([key, answer]) => [key, answer.file])) }
1780
1216
  }
1781
1217
 
1782
- // Every candidate module `at`'s imports offer for `name` as seen `seeing` levels into it, in
1783
- // written order -- `{ answer | import, through, leaves }`: what its `use` items bind (as imports
1784
- // to follow, see pick), then what each module its globs reach has (hasAll), under the glob's cfg
1785
- // leaves too. The list depends on the crate's structure alone, so it is built once per (at,
1786
- // name, seeing) and shared by every asker, who picks the first candidate compatible with its own
1787
- // cfgs -- and built lazily (Lazy): a closure holds hundreds of modules, and the first few usually
1788
- // answer. A list built while the closure was still being resolved (an import cycle, see
1789
- // memoized) is not kept.
1218
+ // Built lazily: a closure holds hundreds of modules, the first few answer.
1790
1219
  function providedAll(root, at, name, seeing, ctx) {
1791
1220
  const memo = nestedMap(ctx.provided, root)
1792
1221
  const key = `${at}\0${name}\0${seeing}`
@@ -1797,26 +1226,15 @@ function providedAll(root, at, name, seeing, ctx) {
1797
1226
  ctx.walking--
1798
1227
  list = new Lazy(function* () {
1799
1228
  yield* importsOf(root, at, name, seeing, ctx)
1800
- // At the crate root, a `#[macro_export]` macro is a binding of the name too, in the macro
1801
- // namespace (`ns`): a `pub use inner::m;` of another build beside it (serde's docsrs-only
1802
- // copy of serde_core's macros) is ranked against it by their cfgs.
1803
- if (at === 'crate') for (const m of ctx.exportedMacros?.get(root)?.get(name) ?? NONE) yield { answer: { kind: 'item', file: m.file }, leaves: m.leaves, ns: 'macro' }
1804
- // The module's own items rank with its named imports -- two explicit bindings of one name
1805
- // are rustc's error unless their cfgs differ (tokio's `imp` re-exports std's `AtomicU64` in
1806
- // one variant file and defines its own in the other) -- ahead of what globs bring in, which
1807
- // never shadows them. The module's tree file first; visibility is the path's to break.
1229
+ // A `#[macro_export]` macro ranks by cfgs against a `pub use inner::m;` beside it (serde's docsrs-only copies).
1230
+ if (at === 'crate') for (const m of ctx.exportedMacros.get(root)?.get(name) ?? NONE) yield { answer: { kind: 'item', file: m.file }, leaves: m.leaves, ns: 'macro' }
1231
+ // Own items rank with named imports, ahead of what globs bring in, which never shadows them.
1808
1232
  const defs = ctx.defined.get(root)?.get(at)?.get(name)
1809
- if (defs !== undefined) {
1810
- const own = ctx.trees.get(root).get(at)
1811
- for (const d of defs) if (d.file === own) yield { answer: { kind: 'item', file: d.file }, leaves: d.leaves, ns: d.ns }
1812
- for (const d of defs) if (d.file !== own) yield { answer: { kind: 'item', file: d.file }, leaves: d.leaves, ns: d.ns }
1813
- }
1814
- // Of the closure's modules (hundreds, in libc), only the ones with something of the name
1815
- // (`having`), in closure order.
1233
+ if (defs !== undefined) yield* defCandidates(root, at, defs, ctx)
1234
+ // Of the closure's modules (hundreds, in libc), only the ones with something of the name.
1816
1235
  const having = ctx.having.get(root)?.get(name)
1817
1236
  if (having === undefined) return
1818
- const positions = []
1819
- for (const module of having) for (const i of closureIndex(closure).get(module) ?? NONE) positions.push(i)
1237
+ const positions = [...having].flatMap((module) => closureIndex(closure).get(module) ?? NONE)
1820
1238
  for (const i of positions.toSorted((x, y) => x - y)) {
1821
1239
  const reached = closure[i]
1822
1240
  for (const c of hasAll(root, reached.module, name, reached.seeing, ctx)) yield { ...c, through: reached.through, leaves: unionLeaves(reached.leaves, c.leaves, ctx.leafSets) }
@@ -1827,19 +1245,11 @@ function providedAll(root, at, name, seeing, ctx) {
1827
1245
  return list
1828
1246
  }
1829
1247
 
1830
- // A closure's entries by module (one may be reached at several depths), kept on the closure.
1831
- function closureIndex(closure) {
1832
- if (closure.index === undefined) {
1833
- closure.index = new Map()
1834
- closure.forEach((e, i) => (closure.index.get(e.module) ?? closure.index.set(e.module, []).get(e.module)).push(i))
1835
- }
1836
- return closure.index
1837
- }
1248
+ // A closure's entry positions by module (one may be reached at several depths).
1249
+ const closureIndex = (closure) => (closure.index ??= Map.groupBy(closure.keys(), (i) => closure[i].module))
1838
1250
 
1839
- // A list materialized from a generator as far as it is read (`get(i)`, undefined past the end).
1840
- // Read again while its generator is producing an element (the element's own lookups led back to
1841
- // the same question), it ends there for that reader, and the computation under way is marked hit
1842
- // (see memoized) so nothing computed on that short view is kept.
1251
+ // A list materialized from a generator as far as it is read. Read again while producing an element, it ends
1252
+ // there for that reader and marks the computation under way hit, so nothing built on that short view is kept.
1843
1253
  class Lazy {
1844
1254
  constructor(gen, ctx) {
1845
1255
  this.gen = gen
@@ -1876,47 +1286,36 @@ class Lazy {
1876
1286
  }
1877
1287
  }
1878
1288
 
1879
- // Everything module `at` has under `name` for one seeing `seeing` levels into it -- what a glob
1880
- // into the module brings in -- as candidates in written order: a child module (visible that far),
1881
- // the items it defines (the module's own tree file first: an inline `mod imp { }` under one cfg
1882
- // beside a `mod imp;` file under another may both define it), at the crate root a `#[macro_export]`
1883
- // macro (an item of the root's namespace: zerocopy's `use super::*;` from the root brings
1884
- // `into_inner!` in), and its `use` items binding the name (importsOf).
1289
+ // What a glob into `at` brings in under `name`, for one seeing `seeing` levels into it, in written order.
1885
1290
  function hasAll(root, at, name, seeing, ctx) {
1886
- // Asked for every module a closure reaches, most of which have nothing of the name.
1887
1291
  const child = ctx.children.get(root)?.get(at)?.has(name) === true
1888
1292
  const defs = ctx.defined.get(root)?.get(at)?.get(name)
1889
- const macros = at === 'crate' ? ctx.exportedMacros?.get(root)?.get(name) : undefined
1293
+ const macros = at === 'crate' ? ctx.exportedMacros.get(root)?.get(name) : undefined
1890
1294
  const imports = importsOf(root, at, name, seeing, ctx)
1295
+ // Asked for every module a closure reaches, most of which have nothing of the name.
1891
1296
  if (!child && defs === undefined && macros === undefined && imports.length === 0) return NONE
1892
1297
  const out = []
1893
1298
  if (child) {
1894
1299
  const sub = `${at}::${name}`
1895
1300
  if ((ctx.modScope.get(root)?.get(sub) ?? 1) <= seeing) out.push({ answer: { kind: 'module', modulePath: sub, file: ctx.trees.get(root).get(sub) }, leaves: NO_LEAVES })
1896
1301
  }
1897
- if (defs !== undefined) {
1898
- const own = ctx.trees.get(root).get(at)
1899
- for (const d of defs) if (d.scopeDepth <= seeing && d.file === own) out.push({ answer: { kind: 'item', file: d.file }, leaves: d.leaves, ns: d.ns })
1900
- for (const d of defs) if (d.scopeDepth <= seeing && d.file !== own) out.push({ answer: { kind: 'item', file: d.file }, leaves: d.leaves, ns: d.ns })
1901
- }
1302
+ if (defs !== undefined) out.push(...defCandidates(root, at, defs.filter((d) => d.scopeDepth <= seeing), ctx))
1902
1303
  for (const m of macros ?? NONE) out.push({ answer: { kind: 'item', file: m.file }, leaves: m.leaves })
1903
1304
  out.push(...imports)
1904
1305
  return out
1905
1306
  }
1906
1307
  const NONE = Object.freeze([])
1907
1308
 
1908
- // The file in which module `at` defines item `name` (a `struct`, `fn`, … under cfgs the asker's
1909
- // allow; in the type namespace when `ns` is `type`, see pick), or null -- or `{ file,
1910
- // alternatives }` when several files define it, none for certain (see withAlternatives; itemAt
1911
- // makes an answer of either).
1309
+ function defCandidates(root, at, defs, ctx) {
1310
+ const own = ctx.trees.get(root).get(at)
1311
+ return [...defs.filter((d) => d.file === own), ...defs.filter((d) => d.file !== own)].map((d) => ({ answer: { kind: 'item', file: d.file }, leaves: d.leaves, ns: d.ns }))
1312
+ }
1313
+
1314
+ // Ranked as pick ranks candidates, own tree file first; `{ file, alternatives }` when several may, none for certain.
1912
1315
  function definedIn(root, at, name, ctx, asker, ns = null) {
1913
1316
  const defs = ctx.defined.get(root)?.get(at)?.get(name)
1914
1317
  if (defs === undefined) return null
1915
1318
  const own = ctx.trees.get(root).get(at)
1916
- // Ranked as pick ranks candidates -- sure, then compatible under no custom cfg, then doubtful,
1917
- // then dead in the asker's build -- the module's own tree file first within a rank, else the
1918
- // first written.
1919
- // Several of the middle rank, none sure: each applies under its own cfgs (see withAlternatives).
1920
1319
  let best = null
1921
1320
  let bestRank = Infinity
1922
1321
  const maybes = []
@@ -1938,36 +1337,25 @@ function definedIn(root, at, name, ctx, asker, ns = null) {
1938
1337
  return best
1939
1338
  }
1940
1339
 
1941
- // What module `at` (spread over the files of cfg variants, `#[cfg_attr(unix, path = …)]`) makes
1942
- // of `name` for a path written in `file`, that file's own imports first: its `use` binding the
1943
- // name, then its own globs; only then the module's imports at large (providedAll). For a module
1944
- // held in one file the two agree, so the shortcut is taken only when they may not. Memoized like
1945
- // providedAll: a `use log;` in such a file asks for `log` in its own module, which is this import.
1340
+ // For a module spread over cfg variant files (`#[cfg_attr(unix, path = …)]`): the asking file's own `use`s first.
1946
1341
  function providedFrom(root, at, name, ctx, file, asker, ns = null) {
1947
1342
  const of = ctx.imports.get(root)?.get(at)
1948
- // The files holding the module's imports, gathered once (they are all in place by now).
1949
- const files = of === undefined ? null : (of.files ??= new Set([...of.globs, ...[...of.named.values()].flat()].map((im) => im.file)))
1343
+ const files = of === undefined ? null : (of.files ??= new Set(importsIn(of).map((im) => im.file)))
1950
1344
  if (files === null || files.size === (files.has(file) ? 1 : 0)) return provided(root, at, name, at, ctx, asker, ns)
1951
1345
  const memo = nestedMap(ctx.provided, root)
1952
1346
  const seeing = at.split('::').length
1953
1347
  const key = `${at}\0${name}\0@${file}`
1954
1348
  let list = memo.get(key)
1955
1349
  if (list === undefined) {
1956
- // Resolving this file's glob sources may ask this very question (`pub use sibling::*;` beside
1957
- // `use super::*;`): while the list is in the making it reads as nothing, and is not kept then.
1350
+ // Resolving this file's glob sources may ask this very question: meanwhile it reads as nothing, and isn't kept.
1958
1351
  const depth = ctx.walking++
1959
1352
  memo.set(key, { depth })
1960
- const own = [] // this file's globs, with their sources
1961
- for (const im of of.globs) {
1962
- if (im.file !== file) continue
1963
- const source = globSource(im, root, ctx)
1964
- if (source?.kind === 'module') own.push([im, source.modulePath])
1965
- }
1353
+ const own = of.globs.filter((im) => im.file === file).map((im) => [im, globSource(im, root, ctx)]).filter(([, s]) => s?.kind === 'module')
1966
1354
  const cyclic = ctx.hits.delete(depth)
1967
1355
  ctx.walking--
1968
1356
  list = new Lazy(function* () {
1969
1357
  yield* importsOf(root, at, name, seeing, ctx, file)
1970
- for (const [im, module] of own) {
1358
+ for (const [im, { modulePath: module }] of own) {
1971
1359
  const sees = commonDepth(at, module)
1972
1360
  for (const c of hasAll(root, module, name, sees, ctx)) yield { ...c, through: im.file, leaves: unionLeaves(im.leaves, c.leaves, ctx.leafSets) }
1973
1361
  for (const c of providedAll(root, module, name, sees, ctx)) yield { ...c, through: im.file, leaves: unionLeaves(im.leaves, c.leaves, ctx.leafSets) }
@@ -1983,40 +1371,29 @@ function providedFrom(root, at, name, ctx, file, asker, ns = null) {
1983
1371
  return pick(list, root, ctx, asker, ns)
1984
1372
  }
1985
1373
 
1986
- // The `use` items of module `at` binding `name`, visible to one seeing `seeing` levels into it,
1987
- // as candidates to follow (pick), in written order, each with the file holding the import as
1988
- // `through`. Several may bind it (`#[cfg(unix)] use a::X;` beside `#[cfg(windows)] use b::X;`).
1989
- // `onlyFile` restricts to the imports written in that file: the module may be spread over cfg
1990
- // variants (`#[cfg_attr(unix, path = …)]`), each binding the name its own way, and a path written
1991
- // in one of them means that one's.
1374
+ // `onlyFile`: for a module spread over cfg variant files, each binding the name its own way.
1992
1375
  function importsOf(root, at, name, seeing, ctx, onlyFile) {
1993
1376
  const named = ctx.imports.get(root)?.get(at)?.named.get(name)
1994
1377
  if (named === undefined) return NONE
1995
- const out = []
1996
- for (const im of named) {
1997
- if (im.scopeDepth > seeing || (onlyFile !== undefined && im.file !== onlyFile)) continue
1998
- out.push({ import: im, through: im.file, leaves: im.leaves })
1999
- }
2000
- return out
1378
+ return named.filter((im) => im.scopeDepth <= seeing && (onlyFile === undefined || im.file === onlyFile)).map((im) => ({ import: im, through: im.file, leaves: im.leaves }))
2001
1379
  }
2002
1380
 
2003
- // The modules of this crate that the globs of module `at` visible to one seeing `seeing` levels
2004
- // into it reach, transitively (a glob's target module's own globs, as seen from the module whose
2005
- // glob named it), depth-first in written order, each `{ module, seeing, through }`: how deep the
2006
- // module naming it sees into it, and the file holding the glob of `at` it was reached through.
2007
- // Computed once per (at, seeing), a settled closure of a module reached standing in for walking
2008
- // on from it; a closure asked for while it is being computed (a glob into a module whose own
2009
- // imports name the glob's source) is a cycle, and reads as empty.
1381
+ // Depth-first in written order; `through` is the file holding `at`'s glob. A settled closure stands in for walking on.
2010
1382
  function globClosure(root, at, seeing, ctx) {
2011
1383
  const memo = nestedMap(ctx.closures, root)
2012
1384
  const key = `${at}\0${seeing}`
2013
- return memoized(memo, key, ctx, () => {
1385
+ const hit = memo.get(key)
1386
+ // Asked again while being computed (a cycle): empty for now, and the entry in progress is marked hit (by its
1387
+ // depth in `ctx.walking`); the same bookkeeping serves globSource, pick and Lazy.
1388
+ if (hit?.depth !== undefined) {
1389
+ ctx.hits.add(hit.depth)
1390
+ return []
1391
+ }
1392
+ if (hit !== undefined) return hit.value
1393
+ const compute = () => {
2014
1394
  const modules = []
2015
- const seen = new Map([[key, null]]) // (module, seeing) → its entry
2016
- // `leaves`: the cfgs of the globs walked through to get here, all of which must hold (a leaf
2017
- // set). A module reached again by another path (libc's `mod primitives`, glob re-exported
2018
- // under a dozen cfgs) is reachable under either -- and so is everything its own globs reach,
2019
- // so a path that widens an entry is walked on from it (`widened`); one that adds nothing ends.
1395
+ const seen = new Map([[key, null]])
1396
+ // A module reached again by another path (libc's `mod primitives`) is reachable under either: walked on from.
2020
1397
  const add = (k, entry) => {
2021
1398
  const known = seen.get(k)
2022
1399
  if (known === undefined) {
@@ -2035,14 +1412,11 @@ function globClosure(root, at, seeing, ctx) {
2035
1412
  if (add(k, { module, seeing: sees, through, leaves }) === 'same') return
2036
1413
  const settled = memo.get(k)
2037
1414
  if (settled !== undefined && settled.depth === undefined) {
2038
- // Already transitive: no walking on from any of them.
2039
1415
  for (const m of settled.value) add(`${m.module}\0${m.seeing}`, { module: m.module, seeing: m.seeing, through, leaves: unionLeaves(leaves, m.leaves, ctx.leafSets) })
2040
1416
  return
2041
1417
  }
2042
1418
  reachGlobs(module, sees, through, leaves)
2043
1419
  }
2044
- // The modules the globs of `module` visible to one seeing `sees` levels into it name, reached
2045
- // through `through` (else each glob's file) under `leaves` and the glob's own.
2046
1420
  const reachGlobs = (module, sees, through, leaves) => {
2047
1421
  for (const im of ctx.imports.get(root)?.get(module)?.globs ?? []) {
2048
1422
  if (im.scopeDepth > sees) continue
@@ -2052,11 +1426,19 @@ function globClosure(root, at, seeing, ctx) {
2052
1426
  }
2053
1427
  reachGlobs(at, seeing, null, NO_LEAVES)
2054
1428
  return modules
2055
- }, { empty: [] })
1429
+ }
1430
+ // What was computed on a cyclic read may be short and is dropped; the hit entry is kept for stabilizeClosures.
1431
+ const depth = ctx.walking++
1432
+ memo.set(key, { depth })
1433
+ const value = compute()
1434
+ const cyclic = ctx.hits.delete(depth)
1435
+ ctx.walking--
1436
+ if (minHit(ctx) < depth) memo.delete(key)
1437
+ else memo.set(key, cyclic ? { value, cyclic, compute } : { value })
1438
+ return value
2056
1439
  }
2057
1440
 
2058
- // What a glob imports from (followImport), asked once per glob rather than once per closure it
2059
- // is part of -- unless the answer came out of an import cycle still being resolved.
1441
+ // Kept on the glob, unless the answer came out of an import cycle still being resolved.
2060
1442
  function globSource(im, root, ctx) {
2061
1443
  if (im.source !== undefined) return im.source
2062
1444
  const depth = ctx.walking++
@@ -2066,15 +1448,10 @@ function globSource(im, root, ctx) {
2066
1448
  return source
2067
1449
  }
2068
1450
 
2069
- // What an import leads to: its path resolved as written in its module, else the crate its lead
2070
- // names -- the sysroot's (`use core::fmt`: an item, living here as far as the bundle knows), an
2071
- // in-tree one, or null for one that isn't (nor anything the tree can model).
1451
+ // Else the crate its lead names: a sysroot one is an item, living here as far as the bundle knows.
2072
1452
  function followImport(im, root, ctx, asker, ns = null) {
2073
1453
  if (!im.absolute) {
2074
1454
  const r = walkPath(im.segments, root, im.module, ctx, { file: im.file, asker, ns })
2075
- // A path into another crate by a name the module binds to it -- the crate itself, through
2076
- // its `use dep::{dep, mac}` too (`dep` the macro of the same name) -- followed as a macro:
2077
- // the macro there (macroOfCrate).
2078
1455
  if (r !== null && ns === 'macro' && r.kind === 'crate' && r.rest?.length > 0) return macroOfCrate(r.file, r.rest, ctx) ?? r
2079
1456
  if (r !== null) return r
2080
1457
  }
@@ -2082,17 +1459,11 @@ function followImport(im, root, ctx, asker, ns = null) {
2082
1459
  if (NON_CRATE_LEADS.has(head)) return { kind: 'item', file: im.file }
2083
1460
  const target = ctx.resolveCrate(head, im.file)
2084
1461
  if (!target) return null
2085
- // A macro of another crate (`use dep::mac;`), followed as one: the file defining it (macroOfCrate).
2086
1462
  const macro = ns === 'macro' && im.segments.length > 1 ? macroOfCrate(target, im.segments.slice(1), ctx) : null
2087
1463
  return macro ?? { kind: 'crate', name: head, file: target }
2088
1464
  }
2089
1465
 
2090
- // The macro `rest` names in the in-tree crate whose root is `target` (`use dep::mac;`,
2091
- // `dep::mac!(…)`, a `#[macro_use] extern crate`'s): looked up from that crate's root as a path
2092
- // there is, in the macro namespace -- each `#[macro_export]` definition under its file's cfgs
2093
- // (serde's docsrs-only copies), a `pub use inner::mac;` followed on into `inner` -- the cfgs
2094
- // judged by that root's own build. `{ kind: 'item', file, macroName, alternatives? }`, the file
2095
- // defining it by the name it defines it under, or null when none does.
1466
+ // Looked up from the crate's root under its own build, by the name the macro is defined under.
2096
1467
  function macroOfCrate(target, rest, ctx) {
2097
1468
  if (!ctx.trees.has(target)) {
2098
1469
  const file = rest.length === 1 ? ctx.macros.get(target)?.get(rest[0]) : undefined
@@ -2101,57 +1472,38 @@ function macroOfCrate(target, rest, ctx) {
2101
1472
  const r = walkPath(['crate', ...rest], target, 'crate', ctx, { ns: 'macro', file: target, asker: ctx.files.get(target)?.asker })
2102
1473
  if (r === null || r === VALUE_ONLY || r.kind !== 'item') return null
2103
1474
  const macroName = r.macroName ?? rest.at(-1)
2104
- if (ctx.fileMacros.get(r.file)?.has(macroName) !== true) return null
2105
- const alternatives = r.alternatives === undefined ? undefined : new Map([...r.alternatives].filter(([, f]) => ctx.fileMacros.get(f)?.has(macroName) === true))
2106
- return { kind: 'item', file: r.file, macroName, ...(alternatives?.size > 1 ? { alternatives } : {}) }
1475
+ if (!definesMacro(ctx, r.file, macroName)) return null
1476
+ const alternatives = macroAlternatives(ctx, r.alternatives, macroName)
1477
+ return { kind: 'item', file: r.file, macroName, ...(alternatives === undefined ? {} : { alternatives }) }
1478
+ }
1479
+
1480
+ const definesMacro = (ctx, file, name) => ctx.fileMacros.get(file)?.has(name) === true
1481
+ const macroAlternatives = (ctx, alternatives, name) => {
1482
+ const defining = new Map([...(alternatives ?? [])].filter(([, f]) => definesMacro(ctx, f, name)))
1483
+ return defining.size > 1 ? defining : undefined
2107
1484
  }
2108
1485
 
2109
- // An item answer for what definedIn found.
2110
1486
  const itemAt = (def) => (typeof def === 'string' ? { kind: 'item', file: def } : { kind: 'item', file: def.file, alternatives: def.alternatives })
2111
1487
 
2112
- // walkPath's answer for a path naming a value-namespace item (a `fn`, `const` or `static`) where
2113
- // a type or module is wanted: as a prefix (`log::info!` beside a `fn log`), or as the target of
2114
- // an import followed as one (`use crate::util::log; log::info!`). Nothing here -- and not an
2115
- // item of the module either: the lead may still name a crate.
1488
+ // A value where a type or module is wanted (`log::info!` beside a `fn log`): the lead may still name a crate.
2116
1489
  const VALUE_ONLY = Object.freeze({ kind: 'value' })
2117
- // Whether module `at` defines `name` only in the value namespace.
2118
1490
  const onlyValues = (root, at, name, ctx) => {
2119
1491
  const defs = ctx.defined.get(root)?.get(at)?.get(name)
2120
1492
  return defs !== undefined && defs.every((d) => d.ns === 'value')
2121
1493
  }
2122
1494
 
2123
- // Resolve `segments`, a path written in module `from` of `root`'s crate, when its lead anchors it
2124
- // there: `crate::`, `self::`, `super::`, a child module of `from`, a name `from`'s imports provide
2125
- // (`use gen::consts; pub use consts::*;`, `extern crate serde_core as s; s::Value`), or -- for a
2126
- // bare name -- a `macro_rules!` of the file (`macro_rules! helper { … } pub(crate) use helper;`).
2127
- // Null when it doesn't (an item in scope, a crate: the caller looks the lead up as one).
2128
- // Then module by module; a segment naming no module is looked up in the module reached, and the
2129
- // walk goes on from a module that provides, or ends at whatever else it provides. A segment
2130
- // nothing provides is an item of the module reached, in the file defining it when that is known.
2131
- // At the crate root a final segment may name a `#[macro_export]` macro, which lives in the file
2132
- // defining it: `$crate::name!` names the macro before anything else (`ns` is `macro`: a path
2133
- // invoked), any other path the module or import of that name first. In the module the path is
2134
- // written in, `file`'s own imports come first: the module's other files, if any, are cfg variants
2135
- // (providedFrom). `asker` (see cfg compatibility) rules out what can't be compiled together with
2136
- // the path.
1495
+ // Null when the lead doesn't anchor the path in `root`'s crate; a segment nothing provides is an item of the module reached.
2137
1496
  function walkPath(segments, root, from, ctx, options = {}) {
2138
1497
  const branches = []
2139
1498
  const r = walkOnce(segments, root, from, ctx, options, branches)
2140
1499
  return branches.length === 0 || r === null || r === VALUE_ONLY || r.file === undefined ? r : throughBranches(r, branches, root, from, ctx, options)
2141
1500
  }
2142
- // The rest of a path through each module a segment may name (`branches`, see withAlternatives:
2143
- // `{ keyed, rest }`, the segments after it): `r`, the answer through the first, with the files the
2144
- // others lead to as its `alternatives` -- each module's walked on from the crate root, an item's
2145
- // the item's file -- under the key of the module they go through.
1501
+ // The files the path leads to through each other module a segment may name become `alternatives` of `r`.
2146
1502
  function throughBranches(r, branches, root, from, ctx, options) {
2147
1503
  const alternatives = new Map()
2148
1504
  const note = (key, answer) => {
2149
1505
  for (const [k, f] of answer.alternatives ?? [[null, answer.file]]) {
2150
- if (f === undefined || [...alternatives.values()].includes(f)) continue
2151
- const base = k === null ? key : `${key}, ${k}`
2152
- let unique = base
2153
- for (let n = 2; alternatives.has(unique); n++) unique = `${base}#${n}`
2154
- alternatives.set(unique, f)
1506
+ if (f !== undefined && ![...alternatives.values()].includes(f)) alternatives.set(freshKey(alternatives, k === null ? key : `${key}, ${k}`), f)
2155
1507
  }
2156
1508
  }
2157
1509
  const [{ keyed: firstKeyed }] = branches
@@ -2170,32 +1522,25 @@ function throughBranches(r, branches, root, from, ctx, options) {
2170
1522
  function walkOnce(segments, root, from, ctx, { ns = null, file, asker } = {}, branches) {
2171
1523
  const tree = ctx.trees.get(root)
2172
1524
  const head = segments[0]
2173
- // A segment with more after it names a module or type-namespace item: a `fn log` doesn't lead
2174
- // `log::info!`, the crate does (`type`, see pick); the last one is in the namespace the caller
2175
- // wants (`type` for an import followed as a path's prefix, `macro` for one invoked).
1525
+ // A segment with more after it names a module or type: a `fn log` doesn't lead `log::info!`.
2176
1526
  const nsOf = (last) => (last ? ns : 'type')
2177
1527
  const isModule = (path, last) => tree.has(path) && !(last && ns === 'macro') // a `mod m` is no `m!`
2178
1528
  const lookup = (at, name, want) => (at === from && file !== undefined ? providedFrom(root, at, name, ctx, file, asker, want) : provided(root, at, name, from, ctx, asker, want))
2179
- let cur = from.split('::')
2180
- let i = 0
1529
+ let cur = head === 'crate' ? ['crate'] : from.split('::')
1530
+ let i = head === 'crate' || head === 'self' ? 1 : 0
2181
1531
  let via
2182
- if (head === 'crate') {
2183
- cur = ['crate']
2184
- i = 1
2185
- } else if (head === 'self') {
2186
- i = 1
2187
- } else if (head !== 'super' && !isModule(`${from}::${head}`, segments.length === 1)) {
1532
+ if (!PATH_KEYWORDS.has(head) && !isModule(`${from}::${head}`, segments.length === 1)) {
2188
1533
  const want = nsOf(segments.length === 1)
2189
1534
  const p = lookup(from, head, want)
2190
1535
  if (p === null || p === VALUE_ONLY) {
2191
- // An item of the module itself (`enum Kind { … } use Kind::*;`), or a macro of the file.
1536
+ // An item of the module itself (`enum Kind { … } use Kind::*;`).
2192
1537
  const own = definedIn(root, from, head, ctx, asker, want)
2193
1538
  if (own !== null) return itemAt(own)
2194
1539
  if (want === 'type' && (p === VALUE_ONLY || onlyValues(root, from, head, ctx))) return VALUE_ONLY
2195
- return segments.length === 1 && file !== undefined && ctx.fileMacros.get(file)?.has(head) === true ? { kind: 'item', file } : null
1540
+ return segments.length === 1 && file !== undefined && definesMacro(ctx, file, head) ? { kind: 'item', file } : null
2196
1541
  }
2197
1542
  via = { modulePath: from, consumed: 0, through: p.through }
2198
- if (p.kind === 'crate') return { ...p, via, rest: segments.slice(1) } // what the path names in that crate (followImport)
1543
+ if (p.kind === 'crate') return { ...p, via, rest: segments.slice(1) }
2199
1544
  if (p.kind !== 'module') return { ...p, via }
2200
1545
  if (p.branches !== undefined) branches.push({ keyed: p.branches, rest: segments.slice(1) })
2201
1546
  cur = p.modulePath.split('::')
@@ -2210,30 +1555,19 @@ function walkOnce(segments, root, from, ctx, { ns = null, file, asker } = {}, br
2210
1555
  const at = cur.join('::')
2211
1556
  const name = segments[i]
2212
1557
  const last = i === segments.length - 1
2213
- // (A macro is looked up as any name is: the crate root's `#[macro_export]` definitions are
2214
- // among its bindings, each under its cfgs -- see providedAll.)
2215
1558
  const macro = at === 'crate' && last && ns !== 'macro' ? ctx.macros.get(root)?.get(name) : undefined
2216
1559
  const child = `${at}::${name}`
2217
1560
  const want = nsOf(last)
2218
1561
  let p
2219
1562
  if (isModule(child, last)) {
2220
- // A child module the asker's build doesn't compile (under a cfg off in it) gives way to what
2221
- // else the module has of the name, if anything; so does one it presumably doesn't (under a
2222
- // custom cfg, doubtful: serde's docsrs-only `mod de` beside the `pub use serde_core::de` of
2223
- // every other build; a `#[cfg(loom)] mod imp` beside the `use other::*` that brings `imp`
2224
- // in), as a doubtful candidate gives way to any other: where the module isn't there, a
2225
- // glob's name is the module's name. Only a module none of whose files may be there: one
2226
- // with a variant the build compiles (`#[cfg_attr(loom, path = "loom.rs")] mod imp;` falls
2227
- // back to imp.rs) is there, and shadows the glob.
2228
- const sets = (ctx.moduleFiles?.get(root)?.get(child) ?? [tree.get(child)]).map((f) => ctx.files.get(f)?.leaves).filter((set) => set !== undefined)
1563
+ // A child module the asker's build doesn't (or presumably doesn't: serde's docsrs-only `mod de` beside
1564
+ // `pub use serde_core::de`) compile gives way to what else the module has of the name.
1565
+ const sets = (ctx.moduleFiles.get(root)?.get(child) ?? [tree.get(child)]).map((f) => ctx.files.get(f)?.leaves).filter((set) => set !== undefined)
2229
1566
  const away = sets.length > 0 && sets.every((set) => deadFor(asker, set) || doubtful(asker, set))
2230
1567
  p = away ? lookup(at, name, want) : null
2231
1568
  if (p === null || p === VALUE_ONLY) {
2232
- // One file there only under cfgs the asker doesn't hold (`#[cfg(not(unix))] mod sys;`
2233
- // beside `#[cfg(unix)] use fallback as sys;`): what else the module binds the name to is a
2234
- // branch of the path too (see throughBranches), under no key of its own. (A `mod` with cfg
2235
- // variants is taken to be there in every build: one of them, a `path` fallback's; and one
2236
- // under a custom cfg the build may set is there, as above.)
1569
+ // One file there only under cfgs the asker doesn't hold (`#[cfg(not(unix))] mod sys;` beside
1570
+ // `#[cfg(unix)] use fallback as sys;`): what else the module binds the name to is a branch too.
2237
1571
  if (!away && sets.length === 1 && !entailed(asker, sets[0]) && !hasCustom(sets[0].leaves, customKey)) {
2238
1572
  const other = lookup(at, name, want)
2239
1573
  if (other !== null && other !== VALUE_ONLY && !(other.kind === 'module' && other.modulePath === child)) branches.push({ keyed: new Map([[cfgKey(cfgTextOf(sets[0].leaves)), { kind: 'module', modulePath: child, file: tree.get(child) }], ['*', other]]), rest: segments.slice(i + 1) })
@@ -2261,43 +1595,28 @@ function walkOnce(segments, root, from, ctx, { ns = null, file, asker } = {}, br
2261
1595
  return { kind: 'module', modulePath, file: tree.get(modulePath), via }
2262
1596
  }
2263
1597
 
2264
- // Whether a glob in module `at`, or in a module its globs reach, may bring `lead` in without the
2265
- // loader seeing it -- a glob into a crate that isn't in-tree (`use syn::*;` brings syn's
2266
- // `punctuated` module in) -- or does bring it in from another in-tree crate: from the module the
2267
- // glob's path names there (`use tokio::sync::*;` brings tokio's `sync::mpsc` in), what it has
2268
- // (hasAll) or its own globs may bring in, in turn. Then a lead nothing here explains is not one to
2269
- // report as a missing crate. A glob the loader sees through -- into a module of this crate, an
2270
- // enum, the sysroot, or an in-tree crate's module without the name -- explains nothing, and no
2271
- // glob brings in its own crate's name. Memoized per (at, lead); asked again while being answered
2272
- // (globs between two crates leading back), it reads as no. Only the globs leading out of the
2273
- // crate matter (opaqueGlobsOf), so a closure of hundreds costs a lookup per such glob.
2274
- // The globs of `root`'s crate that lead out of it -- into a crate that isn't in-tree (source
2275
- // null) or into another in-tree one -- by module, with their sources, found once per crate (and
2276
- // again after stabilizeClosures): the few of a closure's hundreds of modules globMayProvide has
2277
- // to look at.
1598
+ // The few of a closure's hundreds of modules globMayProvide has to look at.
2278
1599
  function opaqueGlobsOf(root, ctx) {
2279
- return cached(ctx.opaqueGlobs, root, () => {
1600
+ return upsert(ctx.opaqueGlobs, root, () => {
2280
1601
  const index = new Map()
2281
1602
  for (const [module, of] of ctx.imports.get(root) ?? []) {
2282
1603
  for (const im of of.globs) {
2283
1604
  if (NON_CRATE_LEADS.has(im.segments[0])) continue
2284
1605
  const source = globSource(im, root, ctx)
2285
- if (source === null || source.kind === 'crate') (index.get(module) ?? index.set(module, []).get(module)).push({ im, source })
1606
+ if (source === null || source.kind === 'crate') upsert(index, module, () => []).push({ im, source })
2286
1607
  }
2287
1608
  }
2288
1609
  return index
2289
1610
  })
2290
1611
  }
2291
1612
 
1613
+ // Whether a glob `at` sees may bring `lead` in unseen (`use syn::*;`) or does from another in-tree crate: then a
1614
+ // lead nothing explains is no missing crate. Asked again while being answered, it reads as no.
2292
1615
  function globMayProvide(root, at, lead, ctx) {
2293
1616
  const memo = nestedMap(ctx.opaque, root)
2294
1617
  const key = `${at}\0${lead}`
2295
1618
  if (memo.has(key)) return memo.get(key)
2296
1619
  memo.set(key, false)
2297
- // The globs leading out of the crate that `at` sees -- its own and its closure's, at the depth
2298
- // each module is seen (a module may be reached at several) -- gathered once per `at`: the ones
2299
- // into crates not in-tree by the crate name they bring nothing of (their own), the ones into
2300
- // in-tree crates with their sources.
2301
1620
  const reachKey = `${at}\0`
2302
1621
  let reach = memo.get(reachKey)
2303
1622
  if (reach === undefined) {
@@ -2317,15 +1636,12 @@ function globMayProvide(root, at, lead, ctx) {
2317
1636
  }
2318
1637
  memo.set(reachKey, reach)
2319
1638
  }
2320
- // What another crate's module `module` (root `file`) makes visible under `lead`, as far as the
2321
- // bundle sees: anything, for a crate not in-tree; else what the module has, what its `pub`
2322
- // globs bring in from its own crate (providedAll), or may from a third.
1639
+ // Whether another crate's module (root `file`) may make `lead` visible: anything, for a crate not in-tree.
2323
1640
  const inCrate = (file, module) => !ctx.trees.has(file) || hasAll(file, module, lead, 1, ctx).length > 0 || providedAll(file, module, lead, 1, ctx).get(0) !== undefined || globMayProvide(file, module, lead, ctx)
2324
1641
  let answer = reach.unseen.size > (reach.unseen.has(lead) ? 1 : 0)
2325
1642
  for (const { im, source } of reach.crates) {
2326
1643
  if (answer || im.segments[0] === lead) break
2327
- // The glob's path on from the crate it names (`sync` of `use tokio::sync::*;`), when the
2328
- // lead itself named the crate (no `via`: the path wasn't anchored in this crate first).
1644
+ // `sync` of `use tokio::sync::*;`, when the lead itself named the crate (no `via`).
2329
1645
  const rest = source.via === undefined ? im.segments.slice(1) : []
2330
1646
  const there = rest.length === 0 || !ctx.trees.has(source.file) ? { kind: 'module', modulePath: 'crate' } : walkPath(['crate', ...rest], source.file, 'crate', ctx)
2331
1647
  if ((there?.kind === 'module' && inCrate(source.file, there.modulePath)) || (there?.kind === 'crate' && inCrate(there.file, 'crate'))) answer = true
@@ -2334,66 +1650,51 @@ function globMayProvide(root, at, lead, ctx) {
2334
1650
  return answer
2335
1651
  }
2336
1652
 
2337
- // Resolve one path reference made in `file` to what it names in the bundle (see walkPath): a
2338
- // module or item of the same crate, an in-tree crate (`{ kind: 'crate', name, file }`, through
2339
- // `resolveCrate` when the lead names one, by its own name or as the crate root's `extern crate x
2340
- // as y;` aliases it, `::y::…` included -- this crate's own root for `extern crate self as y;`),
2341
- // an exported macro named by a one-segment path written at the crate root, where such a macro is
2342
- // an item (`pub use anyhow as format_err;` in the anyhow crate's lib.rs; a child module reaches it
2343
- // as `crate::anyhow`), or `{ kind: 'unresolved', name }` when the lead could name a crate
2344
- // (crateLead) but nothing in-tree did; null for anything else (an item in scope, std, a path
2345
- // above the crate root, a name a glob into another crate may well provide). `bindings` are the names `file`'s imports bind -- in whichever namespace: one bound
2346
- // only as a value (`use crate::util::log;` of a `fn log`) doesn't keep `log::info!` from naming
2347
- // the crate.
1653
+ // `{ kind: 'unresolved' }` for a lead that could name a crate but nothing in-tree did.
2348
1654
  const NO_BINDINGS = new Set()
2349
1655
  function resolvePathRef(ref, file, bindings, ctx) {
2350
1656
  const { segments, absolute, inlinePath } = ref
2351
1657
  if (segments.length === 0) return null
2352
1658
  const here = ctx.files.get(file)
2353
1659
  const from = here ? [here.modulePath, ...inlinePath].join('::') : null
1660
+ const opts = { ns: ref.macroCall ? 'macro' : null, file, asker: here?.asker }
2354
1661
  let valueOnly = false
2355
1662
  if (!absolute && here) {
2356
- const r = walkPath(segments, here.root, from, ctx, { ns: ref.macroCall ? 'macro' : null, file, asker: here.asker })
2357
- if (r === VALUE_ONLY) valueOnly = true // the lead is bound here, but as a value: `log::…` names the crate
2358
- else if (r !== null && r.kind === 'crate' && ref.macroCall && r.rest?.length > 0) return { ...r, macro: macroOfCrate(r.file, r.rest, ctx) ?? undefined } // `d::mac!(…)` with `use dep as d;`
1663
+ const r = walkPath(segments, here.root, from, ctx, opts)
1664
+ if (r === VALUE_ONLY) valueOnly = true // bound here, but as a value: `log::…` names the crate
1665
+ else if (r !== null && r.kind === 'crate' && ref.macroCall && r.rest?.length > 0) return { ...r, macro: macroOfCrate(r.file, r.rest, ctx) ?? undefined }
2359
1666
  else if (r !== null) return r
2360
1667
  }
2361
1668
  const lead = crateLead(ref, valueOnly ? NO_BINDINGS : bindings)
2362
1669
  if (lead === null) return null
2363
1670
  const name = ctx.externPrelude.get(here?.root)?.get(lead) ?? lead
2364
1671
  if (name === 'self') {
2365
- const r = walkPath(['crate', ...segments.slice(1)], here.root, from, ctx, { ns: ref.macroCall ? 'macro' : null, file, asker: here.asker })
1672
+ const r = walkPath(['crate', ...segments.slice(1)], here.root, from, ctx, opts)
2366
1673
  return r === VALUE_ONLY ? null : r
2367
1674
  }
2368
1675
  const target = ctx.resolveCrate(name, file)
2369
- // `dep::mac!(…)`: the crate, and the macro it names there (macroOfCrate).
2370
- if (target) return { kind: 'crate', name, file: target, alternatives: ctx.crateAlternatives?.(name, file), macro: ref.macroCall && segments.length > 1 ? macroOfCrate(target, segments.slice(1), ctx) ?? undefined : undefined }
1676
+ if (target) return { kind: 'crate', name, file: target, alternatives: ctx.crateAlternatives(name, file), macro: ref.macroCall && segments.length > 1 ? macroOfCrate(target, segments.slice(1), ctx) ?? undefined : undefined }
1677
+ // At the crate root an exported macro is an item (anyhow's `pub use anyhow as format_err;`).
2371
1678
  const macro = segments.length === 1 && from === 'crate' ? ctx.macros.get(here.root)?.get(lead) : undefined
2372
1679
  if (macro !== undefined) return { kind: 'item', file: macro }
2373
- // A glob into a crate that isn't in-tree (`use serde::*;`) may bring the lead in -- unless the
2374
- // package declares a dependency of that name, which the lead then is (see declaresCrate): one
2375
- // missing from the bundle is reported whatever the globs beside it.
1680
+ // A glob into a crate that isn't in-tree (`use serde::*;`) may bring the lead in, unless the package declares a
1681
+ // dependency of that name: one missing from the bundle is reported whatever the globs beside it.
2376
1682
  return here && !ctx.declaresCrate(name, file) && globMayProvide(here.root, from, lead, ctx) ? null : { kind: 'unresolved', name }
2377
1683
  }
2378
1684
 
2379
- // `map`: root → name → Set<module>, noting `module` under (root, name).
2380
- const note = (map, root, name, module) => {
2381
- const byName = nestedMap(map, root)
2382
- ;(byName.get(name) ?? byName.set(name, new Set()).get(name)).add(module)
2383
- }
1685
+ const note = (map, root, name, module) => upsert(nestedMap(map, root), name, () => new Set()).add(module)
2384
1686
 
2385
1687
  // Bundle import keys can't contain '/'; a cfg predicate practically never does, but fail safe.
2386
1688
  const cfgKey = (cfg) => (cfg ?? '*').replaceAll('/', '|')
1689
+ const freshKey = (map, base) => {
1690
+ let key = base
1691
+ for (let k = 2; map.has(key); k++) key = `${base}#${k}`
1692
+ return key
1693
+ }
2387
1694
 
2388
- // Whether `rel` is compiled with `cfg(test)`: a file of a test/bench target (`tests/*.rs`,
2389
- // `benches/*.rs` and their modules), judged against its package dir when a Cargo context knows it.
2390
1695
  const isTestTarget = (rel, ctx) => (ctx ? ctx.isTestTarget(rel) : isTestTargetPath('.', rel))
2391
1696
 
2392
- // The files a macro is in effect invoked from, by name: those invoking it by bare name
2393
- // (`invokers`: name → files), and -- since a bare call in a `macro_rules!` template is made where
2394
- // that macro is invoked (`templateCalls`: name → the names its definitions' templates call) --
2395
- // those invoking a macro whose template calls it, on up: what rustc resolves the macro's include
2396
- // paths against, the outermost call site.
1697
+ // A call in a template is made where that macro is invoked: the outermost call site, as rustc resolves it.
2397
1698
  function effectiveInvokers(name, invokers, templateCalls, seen = new Set()) {
2398
1699
  if (seen.has(name)) return new Set()
2399
1700
  seen.add(name)
@@ -2401,47 +1702,33 @@ function effectiveInvokers(name, invokers, templateCalls, seen = new Set()) {
2401
1702
  for (const [caller, called] of templateCalls) if (called.has(name)) for (const f of effectiveInvokers(caller, invokers, templateCalls, seen)) out.add(f)
2402
1703
  return out
2403
1704
  }
2404
- // Note what file `path`'s scan (`items`) invokes, by bare name or by path (`crate::m!()`, matched by
2405
- // name: a `$crate::m!` resolves where the macro is invoked as surely as `m!`), and what its
2406
- // `macro_rules!` templates call, for effectiveInvokers.
1705
+ // Calls by path are matched by name: a `$crate::m!` resolves where the macro is invoked as surely as `m!`.
2407
1706
  function noteMacroCalls(invokers, templateCalls, path, items) {
2408
- for (const name of [...items.invocations, ...items.pathInvocations ?? []]) (invokers.get(name) ?? invokers.set(name, new Set()).get(name)).add(path)
2409
- for (const m of items.macros) for (const name of [...m.calls, ...m.pathCalls ?? []]) (templateCalls.get(m.name) ?? templateCalls.set(m.name, new Set()).get(m.name)).add(name)
1707
+ for (const name of [...items.invocations, ...items.pathInvocations]) upsert(invokers, name, () => new Set()).add(path)
1708
+ for (const m of items.macros) for (const name of [...m.calls, ...m.pathCalls]) upsert(templateCalls, m.name, () => new Set()).add(name)
2410
1709
  }
2411
- // The files in effect invoking macro `name` where a `mod` its template declares is declared: of
2412
- // `from`'s package (`packageOf`: file → package) -- a macro of the same name elsewhere is likely
2413
- // another -- or, for a `#[macro_export]` one (`packageOf` null), of any (see buildRustTree).
1710
+ // Of `from`'s package (a macro of the same name elsewhere is likely another), unless `#[macro_export]`ed.
2414
1711
  const hostsOf = (name, from, invokers, templateCalls, packageOf) => [...effectiveInvokers(name, invokers, templateCalls)].filter((f) => packageOf === null || packageOf(f) === packageOf(from))
2415
- // The copies of template `mod` `decl` (written in a `macro_rules!` body, see scanRustItems) a file
2416
- // invoking the macro declares (`items`: its scan): one per inline module it invokes the macro, or a
2417
- // macro whose template calls it, from -- the `mod` in that module, inside the inline modules the
2418
- // body itself opens -- at that invocation (`offset`, for textual macro scope).
1712
+ // The copies of template `mod` `decl` a file invoking its macro declares: one per inline module invoking the
1713
+ // macro (or one whose template calls it), at the first such invocation (`offset`, for textual scope).
2419
1714
  function hostedCopies(decl, items, templateCalls) {
2420
1715
  const names = new Set([decl.template])
2421
1716
  for (let grew = true; grew;) {
2422
1717
  grew = false
2423
1718
  for (const [caller, called] of templateCalls) if (!names.has(caller) && [...called].some((n) => names.has(n))) grew = names.add(caller)
2424
1719
  }
2425
- const sites = [...names].flatMap((n) => items.callSites?.get(n) ?? []).toSorted((a, b) => a.offset - b.offset)
1720
+ const sites = [...names].flatMap((n) => items.callSites.get(n) ?? []).toSorted((a, b) => a.offset - b.offset)
2426
1721
  const byModule = new Map()
2427
1722
  for (const site of sites) if (!byModule.has(site.inlinePath.join('::'))) byModule.set(site.inlinePath.join('::'), site)
2428
1723
  if (byModule.size === 0) byModule.set('', { offset: Infinity, inlinePath: [], inlineDirs: [] })
2429
- const own = (list) => list.slice(decl.templateDepth ?? 0)
1724
+ const own = (list) => list.slice(decl.templateDepth)
2430
1725
  return [...byModule.values()].map((site) => ({ ...decl, inlinePath: [...site.inlinePath, ...own(decl.inlinePath)], inlineDirs: [...site.inlineDirs, ...own(decl.inlineDirs)], offset: site.offset }))
2431
1726
  }
2432
- // TEMPLATE_MACROS less the ones a package defines itself (`own`), one object per set of them.
2433
- const templatesLessMemo = new Map()
2434
- const templatesLess = (own) => {
2435
- const key = [...own].toSorted().join()
2436
- return templatesLessMemo.get(key) ?? templatesLessMemo.set(key, new Set([...TEMPLATE_MACROS].filter((name) => !own.has(name)))).get(key)
2437
- }
1727
+ const templatesLess = memoized((own) => new Set([...TEMPLATE_MACROS].filter((name) => !own.has(name))), (own) => [...own].toSorted().join())
2438
1728
 
2439
- // The compile units a file is compiled as (see cargo.js unitKey): what `units` recorded, else
2440
- // the target's alone.
2441
1729
  const DEFAULT_UNITS = new Set([TARGET_UNIT])
2442
1730
  const unitsOf = (units, rel) => units?.get(rel) ?? DEFAULT_UNITS
2443
- // Add each of `more` to `map`'s set for `file` (the units it is compiled as, the crates it is
2444
- // compiled in); whether that is news.
1731
+ // Whether adding `more` to `map`'s set for `file` is news.
2445
1732
  function addTo(map, file, more) {
2446
1733
  const have = map.get(file)
2447
1734
  if (have === undefined) {
@@ -2452,42 +1739,33 @@ function addTo(map, file, more) {
2452
1739
  for (const u of more) have.add(u)
2453
1740
  return true
2454
1741
  }
2455
- // `units` (a new map for none) with each entry it doesn't name compiled as its package is
2456
- // (unitOfCrate): a proc-macro crate's for the host, as cargo builds it, any other's for the target.
1742
+ // Each entry `units` doesn't name is compiled as its package is: a proc-macro crate for the host, as cargo builds it.
2457
1743
  function withEntryUnits(units, entries, ctx) {
2458
1744
  const out = units ?? new Map()
2459
1745
  for (const e of entries) if (!out.has(e)) out.set(e, new Set([ctx?.unitOfCrate(e, TARGET_UNIT) ?? TARGET_UNIT]))
2460
1746
  return out
2461
1747
  }
2462
1748
 
2463
- // The build `rel` is scanned under (scanRustItems' options): per unit it is compiled as, its
2464
- // crate's features in that unit's feature context -- on for certain, and on maybe -- and the cfg
2465
- // set of the platform the unit compiles for, as far as the Cargo context `ctx` knows them; and
2466
- // whether it is test code. A build script is compiled for the host, whose cfgs the loader knows
2467
- // only when the target is the host; a crate built both for the target and, as a
2468
- // build-dependency, for the host is scanned under both. Interned per (package, test flag,
2469
- // units), so a scan of the same build is reused (cachedScan compares identity).
2470
- const buildMemo = new WeakMap() // ctx -> key -> build
1749
+ // scanRustItems' options for `rel`, interned per (package, test, units) so a scan of the same build is reused.
1750
+ const buildMemo = new WeakMap()
2471
1751
  const NO_CTX = {}
2472
1752
  function buildOf(rel, ctx, units = null) {
2473
1753
  const test = isTestTarget(rel, ctx)
2474
1754
  const set = unitsOf(units, rel)
2475
1755
  const key = `${ctx?.packageInfo(rel)?.dir ?? ''}\0${test}\0${[...set].toSorted().join()}`
2476
1756
  const memo = nestedMap(buildMemo, ctx ?? NO_CTX)
2477
- return cached(memo, key, () => {
1757
+ return upsert(memo, key, () => {
2478
1758
  const one = (unit) => ({ features: ctx?.featuresFor(rel, unit) ?? null, maybeFeatures: ctx?.maybeFeaturesFor(rel, unit) ?? null, target: ctx?.platformOf(unit)?.cfgs ?? null })
2479
1759
  const each = [...set].map(one)
2480
1760
  return each.length === 1 ? { ...each[0], test } : { units: each, test }
2481
1761
  })
2482
1762
  }
2483
1763
 
2484
- // Scan results per `sources` map, so the tree pass reuses the walk's scan of each file when it
2485
- // ran under the same build (one interned object, see buildOf) on the same content (a caller may
2486
- // edit a file in a map it reuses).
1764
+ // The tree pass reuses the walk's scan under the same build and content (a caller may edit a map it reuses).
2487
1765
  const scanCache = new WeakMap()
2488
1766
  function cachedScan(sources, path, content, build, templates = undefined) {
2489
- const cache = cached(scanCache, sources, () => new Map())
2490
- const key = templates === undefined ? path : `${path}\0${[...templates].join()}` // a rescan with fewer templates, kept beside the plain scan
1767
+ const cache = upsert(scanCache, sources, () => new Map())
1768
+ const key = templates === undefined ? path : `${path}\0${[...templates].join()}`
2491
1769
  const hit = cache.get(key)
2492
1770
  if (hit && hit.content === content && hit.build === build) return hit.items
2493
1771
  const items = scanRustItems(content, templates === undefined ? build : { ...build, templates })
@@ -2495,23 +1773,8 @@ function cachedScan(sources, path, content, build, templates = undefined) {
2495
1773
  return items
2496
1774
  }
2497
1775
 
2498
- // Build the triple from already-loaded sources. Two-phase: resolve `mod` edges (defining each
2499
- // crate root's module tree), then path references against it. An unconditional `mod` with no
2500
- // file → `missing` (fatal); cfg-gated/unresolved/self refs are omitted. `roots` are the entries
2501
- // (crate roots by role; main.rs/lib.rs are roots by name regardless); `baseDir` enables crate
2502
- // resolution through Cargo.toml (own lib, path deps), else only vendored crates already in
2503
- // `sources` resolve. Spec shapes in `resolutions`: `mod <name>` (`mod a::<name>` inside inline
2504
- // module `a`) → file, or a Map<cfg predicate | '*', file> when `#[cfg_attr(…, path)]` variants or
2505
- // same-name `#[cfg]`-gated declarations name different files; `<path as written>` → module file;
2506
- // `use <crate>` → crate root. `unresolvedCrates` names the crates `use`/`extern crate` referenced
2507
- // that nothing in-tree satisfied (registry deps that weren't vendored, typically) --
2508
- // informational, never fatal. `wantedRoots` are in-tree crate roots the paths named that
2509
- // `sources` lacks (the walk took their names for bound items): loaded and built again, they
2510
- // complete the bundle. A `cargo` context (cargo.js) may be passed in to share one
2511
- // between the walk and this pass; it also carries each crate's resolved features, which decide
2512
- // `#[cfg(feature = …)]`, and the build target's cfg set when one was given, which decides
2513
- // `#[cfg(unix)]` and the like. `units`: file → the compile units it is compiled as (see
2514
- // collectRustFilesFromDisk); `wantedUnits` gives each wanted root's, for the walk.
1776
+ // `mod` edges (each crate root's module tree), then paths against it. `resolutions`: file → spec (`mod a::b`, a
1777
+ // path as written, `use <crate>`) → file, or a cfg key → file Map; `wantedRoots`: in-tree roots `sources` lacks.
2515
1778
  export function buildRustTree(sources, { roots = [], baseDir = null, cargo = null, formats = null, units: given = null } = {}) {
2516
1779
  const ctx = cargo ?? (baseDir ? createCargoContext(baseDir, { entries: roots }) : null)
2517
1780
  const units = withEntryUnits(given, roots, ctx)
@@ -2520,41 +1783,26 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2520
1783
  const missing = []
2521
1784
  const scanned = new Map()
2522
1785
  for (const [path, content] of sources) {
2523
- if (formats?.has(path)) continue // an `include_str!` / `include_bytes!` asset: carried, not Rust
2524
- scanned.set(path, cachedScan(sources, path, content, buildOf(path, ctx, units)))
2525
- }
2526
- // A package with a `macro_rules!` of a template macro's name (its own `quote!`, in whichever of
2527
- // its files) invokes that one everywhere in the package: no template there, so the files of the
2528
- // package that skipped a body of that name are scanned again with the name dropped from the set
2529
- // (the package: the Cargo context's; without one, a vendored crate's `vendor/<dir>`, else the
2530
- // bundle's).
2531
- const packages = new Map() // path → its package, asked for every import and item (gatesOf)
2532
- const packageOf = (path) => cached(packages, path, () => (ctx ? ctx.packageInfo(path)?.dir ?? '.' : path.startsWith(`${VENDOR_DIR}/`) ? path.split('/', 2).join('/') : '.'))
2533
- const ownTemplates = new Map() // package → the template names it defines
1786
+ if (!formats?.has(path)) scanned.set(path, cachedScan(sources, path, content, buildOf(path, ctx, units)))
1787
+ }
1788
+ // A package defining its own `quote!` invokes that one everywhere in it: files that skipped one are rescanned.
1789
+ const packageOf = memoized((path) => (ctx ? ctx.packageInfo(path)?.dir ?? '.' : path.startsWith(`${VENDOR_DIR}/`) ? path.split('/', 2).join('/') : '.'))
1790
+ const ownTemplates = new Map()
2534
1791
  for (const [path, items] of scanned) {
2535
- for (const m of items.macros) if (TEMPLATE_MACROS.has(m.name)) cached(ownTemplates, packageOf(path), () => new Set()).add(m.name)
1792
+ for (const m of items.macros) if (TEMPLATE_MACROS.has(m.name)) upsert(ownTemplates, packageOf(path), () => new Set()).add(m.name)
2536
1793
  }
2537
1794
  for (const [path, items] of scanned) {
2538
1795
  const own = items.skipped.size > 0 ? ownTemplates.get(packageOf(path)) : undefined
2539
1796
  if (own === undefined || own.isDisjointFrom(items.skipped)) continue
2540
1797
  scanned.set(path, cachedScan(sources, path, sources.get(path), buildOf(path, ctx, units), templatesLess(own)))
2541
1798
  }
2542
- // Bare macro invocations per file, for what a `macro_rules!` body holds: rustc expands it where
2543
- // the macro is invoked -- through the templates that call it, the outermost (effectiveInvokers).
2544
1799
  const invokers = new Map()
2545
1800
  const templateCalls = new Map()
2546
1801
  for (const [path, items] of scanned) noteMacroCalls(invokers, templateCalls, path, items)
2547
- // A `mod` a `macro_rules!` template declares is declared in each module invoking the macro, and
2548
- // its file found from there (serde_core's src/crate_root.rs holds `crate_root! { … pub mod de;
2549
- // … }`, which lib.rs invokes: src/de/mod.rs), in the inline module the invocation is in, by
2550
- // bare name or by path (`crate::m!()`, `$crate::m!()` in another template). Only its own
2551
- // package's invocations count -- a macro of the same name elsewhere is likely another -- unless
2552
- // the macro is `#[macro_export]`ed, and one nothing invokes stays beside its definition. A
2553
- // hosted `mod` stands at the host's invocation (`offset`), for textual macro scope, and keeps the file defining it
2554
- // (`definedIn`): the host's cfgs on mounting that file are its own too (serde's lib.rs mounts
2555
- // core/crate_root.rs under `#[cfg(docsrs)]`).
2556
- const hosted = new Map() // file → the template `mod`s it declares by invoking their macros
2557
- const hostedAway = new Set() // the template `mod`s that moved to their invokers
1802
+ // A `mod` a template declares is declared in each module invoking the macro (serde_core's `crate_root! { … pub
1803
+ // mod de; … }`), of its own package unless `#[macro_export]`ed; it keeps its defining file and mounting cfgs.
1804
+ const hosted = new Map()
1805
+ const hostedAway = new Set()
2558
1806
  for (const [path, items] of scanned) {
2559
1807
  for (const decl of items.mods) {
2560
1808
  if (decl.template === null) continue
@@ -2562,13 +1810,11 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2562
1810
  const hosts = hostsOf(decl.template, path, invokers, templateCalls, exported ? null : packageOf)
2563
1811
  if (hosts.length === 0) continue
2564
1812
  hostedAway.add(decl)
2565
- for (const f of hosts) for (const copy of hostedCopies(decl, scanned.get(f), templateCalls)) (hosted.get(f) ?? hosted.set(f, []).get(f)).push({ ...copy, definedIn: path })
1813
+ for (const f of hosts) for (const copy of hostedCopies(decl, scanned.get(f), templateCalls)) upsert(hosted, f, () => []).push({ ...copy, definedIn: path })
2566
1814
  }
2567
1815
  }
2568
1816
  const modsOf = (path) => [...scanned.get(path).mods.filter((d) => !hostedAway.has(d)), ...(hosted.get(path) ?? [])]
2569
- // An `include!`d file's items join the includer's module (buildModuleTrees), but a `mod` it
2570
- // declares resolves beside the included file itself, as rustc does: like a `#[path]`-loaded
2571
- // file, it owns its directory (pathLoaded).
1817
+ // A `mod` an `include!`d file declares resolves beside it, as rustc does: like a `#[path]`-loaded file's.
2572
1818
  const pathLoaded = new Set()
2573
1819
  for (const [path, items] of scanned) {
2574
1820
  for (const inc of items.includes) {
@@ -2577,55 +1823,41 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2577
1823
  if (file !== null && file !== path && sources.has(file)) pathLoaded.add(file)
2578
1824
  }
2579
1825
  }
2580
- // Files a `#[path]` names own their directory too (see getModuleDir), so find those before
2581
- // resolving any default `mod` lookup; a path-loaded file may itself hold inline modules with
2582
- // `#[path]`s, hence the loop to a fixed point.
1826
+ // Files a `#[path]` names own their directory too, so find those before any default `mod` lookup; a path-loaded
1827
+ // file may itself hold inline modules with `#[path]`s, hence the loop to a fixed point.
2583
1828
  const modOpts = { knownSources: sources, roots: rootSet, pathLoaded }
2584
1829
  const declsOf = (path, decl) => resolveModDecl(decl, path, { ...modOpts, boundary: boundaryOf(path, ctx) })
2585
- for (let grew = true; grew;) {
2586
- grew = false
1830
+ for (let size = -1; size !== pathLoaded.size;) {
1831
+ size = pathLoaded.size
2587
1832
  for (const path of scanned.keys()) {
2588
1833
  for (const decl of modsOf(path)) {
2589
1834
  if (decl.paths.length === 0) continue
2590
- for (const t of declsOf(path, decl)) {
2591
- if (t.explicit && !pathLoaded.has(t.file)) {
2592
- pathLoaded.add(t.file)
2593
- grew = true
2594
- }
2595
- }
1835
+ for (const t of declsOf(path, decl)) if (t.explicit) pathLoaded.add(t.file)
2596
1836
  }
2597
1837
  }
2598
1838
  }
2599
- // The include macros a `macro_rules!` body holds name files relative to each file in effect
2600
- // invoking the macro, recorded there, as `include_str <path>`. Every definition of a name counts
2601
- // (two `macro_rules! m` in two files: both bodies' assets).
2602
- const includeEdges = new Map() // file → [{ kind, path, base, conditional }] from macro templates it invokes
2603
- for (const [, items] of scanned) {
1839
+ // Relative to each file in effect invoking the template; every definition of a name counts.
1840
+ const includeEdges = new Map()
1841
+ for (const items of scanned.values()) {
2604
1842
  for (const m of items.macros) {
2605
1843
  if (m.includes.length === 0) continue
2606
- for (const f of effectiveInvokers(m.name, invokers, templateCalls)) {
2607
- const list = includeEdges.get(f) ?? includeEdges.set(f, []).get(f)
2608
- for (const inc of m.includes) list.push({ ...inc, conditional: true })
2609
- }
1844
+ const incs = m.includes.map((inc) => ({ ...inc, conditional: true }))
1845
+ for (const f of effectiveInvokers(m.name, invokers, templateCalls)) upsert(includeEdges, f, () => []).push(...incs)
2610
1846
  }
2611
1847
  }
2612
- // file → spec → target file → the declarations mounting it, each its cfg (null for an ungated
2613
- // one), the macro body it sits in and -- for a hosted template `mod` -- the declarations here
2614
- // mounting the file defining it (`via`), for the cfg leaves each file carries (buildModuleTrees).
1848
+ // file → spec → target file → the declarations mounting it: each its cfg, the macro body it sits in and, for a
1849
+ // hosted template `mod`, the declarations here mounting its defining file (`via`).
2615
1850
  const mountCfgs = new Map()
2616
1851
  for (const [path, items] of scanned) {
2617
- // spec -> Map<cfg key, file>: each declaration's files under its cfg predicate ('*' when
2618
- // ungated; `<macro>!` for one a macro emits under a cfg the loader can't see, tokio's
2619
- // `cfg_has_atomic_u64! { mod imp; }`), so `#[cfg(unix)] #[path = "u.rs"] mod imp;` beside
2620
- // `#[cfg(windows)] #[path = "w.rs"] mod imp;` keeps both files. The first declaration of a
2621
- // key wins; another file under the same key gets the key numbered (`*#2`) rather than lost.
1852
+ // spec → cfg key → file ('*' ungated, `<macro>!` for one a macro emits under a cfg the loader can't see), so
1853
+ // same-name `mod`s under different cfgs keep each file; another file under a taken key gets it numbered.
2622
1854
  const byCfg = new Map()
2623
- const cfgsHere = mountCfgs.set(path, new Map()).get(path)
1855
+ const cfgsHere = nestedMap(mountCfgs, path)
2624
1856
  for (const decl of modsOf(path)) {
2625
1857
  const spec = `mod ${[...decl.inlinePath, decl.name].join('::')}`
2626
1858
  const targets = declsOf(path, decl)
2627
1859
  if (targets.length === 0) {
2628
- // conditional && unresolved: cfg-gated module, may be compiled out -- tolerated.
1860
+ // A cfg-gated module may be compiled out: tolerated.
2629
1861
  if (!decl.conditional) {
2630
1862
  console.warn(`[loader.rust] Missing module: ${decl.name} from ${path}`)
2631
1863
  missing.push({ spec, from: path })
@@ -2641,18 +1873,15 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2641
1873
  for (let k = 2; keyed.has(key) && keyed.get(key) !== t.file; k++) key = `${base}#${k}`
2642
1874
  if (!keyed.has(key)) keyed.set(key, t.file)
2643
1875
  const via = decl.definedIn === undefined ? undefined : [...cfgsHere.values()].flatMap((m) => m.get(decl.definedIn) ?? [])
2644
- ;(byFile.get(t.file) ?? byFile.set(t.file, []).get(t.file)).push({ cfg, macro: decl.macro, via })
1876
+ upsert(byFile, t.file, () => []).push({ cfg, macro: decl.macro, via })
2645
1877
  }
2646
1878
  }
2647
- // A single file is a flat string -- also when every cfg names the same file (libc's `mod
2648
- // primitives` under a dozen); cfg variants stay a Map.
1879
+ // A single file is a flat string, also when every cfg names the same file (libc's `mod primitives`).
2649
1880
  const specMap = new Map()
2650
1881
  for (const [spec, keyed] of byCfg) {
2651
1882
  const distinct = new Set(keyed.values())
2652
- specMap.set(spec, distinct.size === 1 ? distinct.values().next().value : keyed)
1883
+ specMap.set(spec, distinct.size === 1 ? [...distinct][0] : keyed)
2653
1884
  }
2654
- // `include!("generated/consts.rs")` and the asset macros name a file relative to this one (or
2655
- // to the package root), the ones in a macro body it invokes relative to this one too.
2656
1885
  for (const inc of [...items.includes, ...(includeEdges.get(path) ?? [])]) {
2657
1886
  const file = includeTarget(inc, path, ctx)
2658
1887
  if (file !== null && file !== path && sources.has(file)) specMap.set(`${inc.kind} ${inc.path}`, file)
@@ -2662,32 +1891,24 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2662
1891
  resolutions.set(path, specMap)
2663
1892
  }
2664
1893
 
2665
- // Each package's gate macros (`cfg_x!`), by name: the cfg its definition wraps items in, or null
2666
- // when that can't be read -- or when the package defines the name twice, differently.
1894
+ // Each package's gate macros: null when unreadable, or when the package defines the name twice, differently.
2667
1895
  const gatesByPackage = new Map()
2668
1896
  for (const [path, items] of scanned) {
2669
1897
  for (const m of items.macros) {
2670
1898
  if (m.gate === undefined) continue
2671
- const gates = cached(gatesByPackage, packageOf(path), () => new Map())
1899
+ const gates = upsert(gatesByPackage, packageOf(path), () => new Map())
2672
1900
  gates.set(m.name, gates.has(m.name) && gates.get(m.name) !== m.gate ? null : m.gate)
2673
1901
  }
2674
1902
  }
2675
1903
  const gatesOf = (path) => gatesByPackage.get(packageOf(path)) ?? null
2676
1904
  const { trees, files } = buildModuleTrees(sources, resolutions, rootSet, new Map([...scanned].map(([p, items]) => [p, items.inlineModules])), mountCfgs, gatesOf)
2677
- // What each file asks as: itself, with the cfg leaves it was mounted under (see cfg
2678
- // compatibility), as an interned leaf set (leafSets: every set of this tree, by key).
2679
1905
  const leafSets = new Map()
2680
- const askerSets = new Map() // per leaf set, build and settable cfgs: what every asker of those shares
1906
+ const askerSets = new Map()
2681
1907
  const ids = new WeakMap()
2682
1908
  let nextId = 0
2683
- const idOf = (object) => ids.get(object) ?? ids.set(object, nextId++).get(object)
2684
- // With a file's platform known, its cfgs hold for the file: a `unix` asker never takes a
2685
- // `windows` candidate, a `target_os = "linux"` one prefers the `any(android, linux, …)` branch
2686
- // (see compatible). The platform is the target's for code compiled for it, the host's for a
2687
- // build script (known only when the target is the host); a file compiled for both platforms
2688
- // asks under the cfgs they share, which the loader knows only when they are one.
2689
- const platformLeaves = new Map() // cfg set -> its leaf set
2690
- const leavesOfCfgs = (cfgs) => platformLeaves.get(cfgs) ?? platformLeaves.set(cfgs, leafSet([...cfgs].flatMap(cfgLeaves).filter((l) => TARGET_CFG_KEYS.has(l.key)), leafSets)).get(cfgs)
1909
+ const idOf = (object) => upsert(ids, object, () => nextId++)
1910
+ // Known platform cfgs hold for a file; one compiled for both target and host knows them only when they are one.
1911
+ const leavesOfCfgs = memoized((cfgs) => leafSet([...cfgs].flatMap(cfgLeaves).filter((l) => TARGET_CFG_KEYS.has(l.key)), leafSets))
2691
1912
  const platformCfgsOf = (path) => {
2692
1913
  const each = new Set([...unitsOf(units, path)].map((u) => ctx?.platformOf(u)?.cfgs ?? null))
2693
1914
  return each.size === 1 ? [...each][0] : null
@@ -2696,94 +1917,60 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2696
1917
  f.leaves = leafSet(f.leaves, leafSets)
2697
1918
  const cfgs = platformCfgsOf(path)
2698
1919
  const joined = cfgs === null ? f.leaves : unionLeaves(f.leaves, leavesOfCfgs(cfgs), leafSets)
2699
- // A file the target rules out (tokio's atomic_u64_as_mutex.rs and its submodules under a
2700
- // 64-bit target) asks as it would where it is compiled: under its own cfgs, which the target's
2701
- // contradict. One whose own contradict each other (under `not(all(test, loom))` and `all(loom,
2702
- // test)` both) is compiled nowhere: every candidate is compatible with it, none certain
2703
- // (`contradictory`), so its paths map all of them.
1920
+ // A file the target rules out (tokio's atomic_u64_as_mutex.rs on a 64-bit target) asks under its own cfgs. One
1921
+ // whose own contradict each other is compiled nowhere: every candidate is compatible with it, none certain.
2704
1922
  const set = joined !== f.leaves && cfgExclusive(joined.leaves, joined.leaves) && !cfgExclusive(f.leaves.leaves, f.leaves.leaves) ? f.leaves : joined
2705
- // What the askers of one leaf set, build and settable cfgs share (one object each).
2706
1923
  const build = buildOf(path, ctx, units)
2707
1924
  const settable = ctx?.cfgsSetFor(path) ?? null
2708
1925
  const sharedKey = `${set.key}\0${idOf(build)}\0${settable === null ? '' : idOf(settable)}`
2709
- const shared = askerSets.get(sharedKey) ?? askerSets.set(sharedKey, { keys: new Set(set.leaves.map(leafKey)), compat: new Map(), sure: new Map(), doubt: new Map(), dead: new Map(), texts: new Map(), build, deadHere: deadUnder(set.leaves, build), contradictory: cfgExclusive(set.leaves, set.leaves), custom: customFor(settable) }).get(sharedKey)
1926
+ const shared = upsert(askerSets, sharedKey, () => ({ keys: new Set(set.leaves.map(leafKey)), compat: new Map(), sure: new Map(), doubt: new Map(), dead: new Map(), texts: new Map(), build, deadHere: deadUnder(set.leaves, build), contradictory: cfgExclusive(set.leaves, set.leaves), custom: customFor(settable) }))
2710
1927
  f.asker = { file: path, set, ...shared }
2711
1928
  }
2712
1929
  // Per crate root, each module's child modules by name (every module's parent is in its tree).
2713
1930
  const children = new Map()
2714
1931
  for (const [root, tree] of trees) {
2715
- const byParent = new Map()
1932
+ const byParent = nestedMap(children, root)
2716
1933
  for (const modulePath of tree.keys()) {
2717
1934
  const cut = modulePath.lastIndexOf('::')
2718
- if (cut === -1) continue
2719
- const parent = modulePath.slice(0, cut)
2720
- ;(byParent.get(parent) ?? byParent.set(parent, new Set()).get(parent)).add(modulePath.slice(cut + 2))
1935
+ if (cut !== -1) upsert(byParent, modulePath.slice(0, cut), () => new Set()).add(modulePath.slice(cut + 2))
2721
1936
  }
2722
- children.set(root, byParent)
2723
1937
  }
2724
- // Per crate root, each module's files: a `mod` with cfg variants has several, any of which a
2725
- // build may compile (walkPath asks whether one of them may be there at all).
1938
+ // A `mod` with cfg variants has several files, any of which a build may compile.
2726
1939
  const moduleFiles = new Map()
2727
- for (const [path, f] of files) {
2728
- const byModule = nestedMap(moduleFiles, f.root)
2729
- ;(byModule.get(f.modulePath) ?? byModule.set(f.modulePath, []).get(f.modulePath)).push(path)
2730
- }
2731
- // Per crate root, what a path may continue along where a segment names no module: the imports
2732
- // of each module (by module path: `named` by the name each binds, and `globs`; each with the
2733
- // `file` holding it, its `module`, how deep in the tree the module its visibility reaches sits,
2734
- // `scopeDepth`: 1 for a `pub`, the module's own depth for a plain `use`, and its cfg `leaves`:
2735
- // its file's plus its own cfg's), the items each module defines (`defined`: module → name →
2736
- // [{ file, scopeDepth, ns, leaves }], `ns` the namespace: `value` for a fn/const/static, which no
2737
- // path continues past, `type` for the rest), how far each module's own visibility reaches (`modScope`:
2738
- // module → scopeDepth, for what a glob may bring in) and the `#[macro_export]` macros (by name →
2739
- // the first file; `exportedMacros`: name → each definition, `{ file, leaves }`, its file's leaves
2740
- // and its own cfg's -- serde's docsrs-only copies of serde_core's macros, a crate's per-platform
2741
- // ones). `fileMacros` is file → the `macro_rules!` names it defines. `externPrelude` (root →
2742
- // alias → crate) holds the root's `extern crate x as y;`, in scope crate-wide.
1940
+ for (const [path, f] of files) upsert(nestedMap(moduleFiles, f.root), f.modulePath, () => []).push(path)
1941
+ // What a path may continue along where a segment names no module; `scopeDepth`: how far visibility reaches.
2743
1942
  const imports = new Map()
2744
1943
  const defined = new Map()
2745
1944
  const modScope = new Map()
2746
1945
  const macros = new Map()
2747
- const exportedMacros = new Map() // root -> name -> every `#[macro_export]` definition, `{ file, leaves }`
1946
+ const exportedMacros = new Map()
2748
1947
  const fileMacros = new Map()
2749
1948
  const externPrelude = new Map()
2750
- // An import's or item's leaves: those its file (`here`) was mounted under, its own cfg's, and
2751
- // its gate macro's.
2752
1949
  const itemLeaves = (here, x, path) => unionLeaves(here.leaves, leafSet([...cfgLeaves(x.cfg), ...gateLeaves(x.macro, gatesOf(path))], leafSets), leafSets)
2753
1950
  const scopeDepthOf = (vis, modulePath) => visibilityScope(vis, modulePath).split('::').length
2754
- // Bare `name!` invocations resolve in textual scope, as rustc has it: a `macro_rules!` is in
2755
- // scope from its definition to the end of its file, in the files the `mod`s after it mount (and
2756
- // theirs, on down), and -- when its module is mounted with `#[macro_use]` -- in the mounting
2757
- // file after that `mod`, and on from there. `mounts` is file → `{ parent, offset }`, the `mod`
2758
- // (or `include!`) that first mounted it; `macroUseMods` file → its `#[macro_use] mod`s, each
2759
- // `{ offset, files }`.
1951
+ // Bare `name!` calls resolve in textual scope, as in rustc: a `macro_rules!` is in scope to the end of its file,
1952
+ // in the files the `mod`s after it mount, and past a `#[macro_use] mod` in the mounting file.
2760
1953
  const mounts = new Map()
2761
1954
  const macroUseMods = new Map()
2762
1955
  const filesOf = (target) => (target instanceof Map ? [...target.values()] : [target])
2763
- // In tree order (`files`: each crate's, from its root down, the variants of a `mod` in the order
2764
- // declared), whatever order the sources came in: among several files defining or importing a
2765
- // name for one module, the first is the tree's own, and the answers are the same for any order
2766
- // the caller lists the files in.
1956
+ // In tree order, so among several files defining or importing a name for one module the tree's own comes first.
2767
1957
  for (const [path, here] of files) {
2768
1958
  const items = scanned.get(path)
2769
1959
  if (items === undefined) continue
2770
1960
  for (const im of items.imports) {
2771
- // A `use $crate::…` in a `macro_rules!` template is the invoking module's, wherever that is.
2772
- // (A `use` without `$crate` in one is kept: serde wraps its whole root in a macro, whose
2773
- // `mod lib { pub use core::fmt; … }` is what every file's `use crate::lib::*` names.)
1961
+ // A `use $crate::…` in a template is the invoking module's. (One without `$crate` is kept: serde wraps its
1962
+ // whole root in a macro, whose `mod lib { pub use core::fmt; … }` every file's `use crate::lib::*` names.)
2774
1963
  if (im.template === true) continue
2775
1964
  const modulePath = [here.modulePath, ...im.inlinePath].join('::')
2776
- const byModule = nestedMap(imports, here.root)
2777
- const of = byModule.get(modulePath) ?? byModule.set(modulePath, { named: new Map(), globs: [] }).get(modulePath)
1965
+ const of = upsert(nestedMap(imports, here.root), modulePath, () => ({ named: new Map(), globs: [] }))
2778
1966
  const entry = { ...im, file: path, module: modulePath, scopeDepth: scopeDepthOf(im.vis, modulePath), leaves: itemLeaves(here, im, path) }
2779
1967
  if (im.glob) of.globs.push(entry)
2780
- else if (im.binding !== null) (of.named.get(im.binding) ?? of.named.set(im.binding, []).get(im.binding)).push(entry)
1968
+ else if (im.binding !== null) upsert(of.named, im.binding, () => []).push(entry)
2781
1969
  }
2782
1970
  for (const d of items.defined) {
2783
1971
  const modulePath = [here.modulePath, ...d.inlinePath].join('::')
2784
- const byModule = nestedMap(defined, here.root)
2785
- const names = nestedMap(byModule, modulePath)
2786
- ;(names.get(d.name) ?? names.set(d.name, []).get(d.name)).push({ file: path, scopeDepth: scopeDepthOf(d.vis, modulePath), ns: d.ns, leaves: itemLeaves(here, d, path) })
1972
+ const names = nestedMap(nestedMap(defined, here.root), modulePath)
1973
+ upsert(names, d.name, () => []).push({ file: path, scopeDepth: scopeDepthOf(d.vis, modulePath), ns: d.ns, leaves: itemLeaves(here, d, path) })
2787
1974
  }
2788
1975
  const scopes = nestedMap(modScope, here.root)
2789
1976
  const specMap = resolutions.get(path)
@@ -2796,7 +1983,7 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2796
1983
  const mountedFiles = filesOf(target).filter((f) => f !== path)
2797
1984
  const mount = { parent: path, offset: decl.offset, at: decl.at }
2798
1985
  for (const f of mountedFiles) if (!mounts.has(f)) mounts.set(f, mount)
2799
- if (decl.macroUse && mountedFiles.length > 0) (macroUseMods.get(mount.parent) ?? macroUseMods.set(mount.parent, []).get(mount.parent)).push({ offset: mount.offset, at: mount.at, files: mountedFiles })
1986
+ if (decl.macroUse && mountedFiles.length > 0) upsert(macroUseMods, mount.parent, () => []).push({ offset: mount.offset, at: mount.at, files: mountedFiles })
2800
1987
  }
2801
1988
  for (const [spec, target] of specMap) {
2802
1989
  if (spec.startsWith('include ') && !mounts.has(target) && target !== path) mounts.set(target, { parent: path, offset: Infinity, at: Infinity })
@@ -2808,10 +1995,8 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2808
1995
  fileMacros.set(path, new Set(items.macros.map((m) => m.name)))
2809
1996
  for (const m of items.macros) {
2810
1997
  if (!m.exported) continue
2811
- const byName = nestedMap(macros, here.root)
2812
- if (!byName.has(m.name)) byName.set(m.name, path)
2813
- const defs = nestedMap(exportedMacros, here.root)
2814
- ;(defs.get(m.name) ?? defs.set(m.name, []).get(m.name)).push({ file: path, leaves: itemLeaves(here, m, path) })
1998
+ upsert(nestedMap(macros, here.root), m.name, () => path)
1999
+ upsert(nestedMap(exportedMacros, here.root), m.name, () => []).push({ file: path, leaves: itemLeaves(here, m, path) })
2815
2000
  }
2816
2001
  if (here.modulePath === 'crate') {
2817
2002
  const prelude = nestedMap(externPrelude, here.root)
@@ -2822,8 +2007,7 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2822
2007
  }
2823
2008
  }
2824
2009
  }
2825
- // The macros a file mounted with `#[macro_use]` hands the file mounting it: its own and, in
2826
- // turn, its own `#[macro_use] mod`s' (name → defining file).
2010
+ // The macros a file mounted with `#[macro_use]` hands the file mounting it, its own `#[macro_use] mod`s' too.
2827
2011
  const exportsMemo = new Map()
2828
2012
  const macroExports = (file, guard = new Set()) => {
2829
2013
  if (exportsMemo.has(file)) return exportsMemo.get(file)
@@ -2835,20 +2019,14 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2835
2019
  exportsMemo.set(file, out)
2836
2020
  return out
2837
2021
  }
2838
- // A file's macro events in source order, each `{ offset, at, macros }` (name → defining file):
2839
- // its own `macro_rules!` and what its `#[macro_use] mod`s hand it. `offset` is where an item
2840
- // stands, `at` where it is written: the same for most, the invocation and the place in the
2841
- // template for what a `macro_rules!` emits (serde's `crate_root! { … macro_rules! tri { … } …
2842
- // pub mod de; … }`: `tri` is in scope in `de`, both standing at the invocation).
2843
- const eventsMemo = new Map()
2844
- const fileEvents = (file) => cached(eventsMemo, file, () => [
2022
+ // A file's macro events in source order. `offset` is where an item stands, `at` where it is written: they
2023
+ // differ for what a template emits (serde's `tri`, standing at `crate_root!`).
2024
+ const fileEvents = memoized((file) => [
2845
2025
  ...(scanned.get(file)?.macros ?? []).map((m) => ({ offset: m.offset, at: m.at, macros: new Map([[m.name, file]]) })),
2846
2026
  ...(macroUseMods.get(file) ?? []).map((u) => ({ offset: u.offset, at: u.at, macros: new Map(u.files.flatMap((f) => [...macroExports(f)])) })),
2847
2027
  ].toSorted((a, b) => a.offset - b.offset || a.at - b.at))
2848
2028
  const before = (e, offset, at) => e.offset < offset || (e.offset === offset && e.at < at)
2849
- // The macros in textual scope in `file` before position (`offset`, `at`) -- (Infinity,
2850
- // Infinity): its end -- on top of `scope`, what it sees from above: the later definition of a
2851
- // name shadows the earlier, whichever kind each is.
2029
+ // The later definition of a name shadows the earlier, whichever kind each is.
2852
2030
  const textualScope = (file, offset, at, scope) => {
2853
2031
  const out = new Map(scope)
2854
2032
  for (const e of fileEvents(file)) {
@@ -2857,20 +2035,9 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2857
2035
  }
2858
2036
  return out
2859
2037
  }
2860
- // The file defining the macro `name` in scope at (`offset`, `at`) in `file`: the last event
2861
- // before that point defining it, else what the file sees from above (`scope`); a definition
2862
- // later in the file doesn't reach an earlier call.
2863
- const macroAt = (file, name, offset, at, scope) => {
2864
- const events = fileEvents(file)
2865
- for (let i = events.length - 1; i >= 0; i--) {
2866
- if (!before(events[i], offset, at)) continue
2867
- const where = events[i].macros.get(name)
2868
- if (where !== undefined) return where
2869
- }
2870
- return scope.get(name)
2871
- }
2872
- // What a file sees from above: what its parent sees from above, and the parent's textual scope
2873
- // at the `mod` mounting it.
2038
+ const macroAt = (file, name, offset, at, scope) =>
2039
+ fileEvents(file).findLast((e) => before(e, offset, at) && e.macros.has(name))?.macros.get(name) ?? scope.get(name)
2040
+ // What a file sees from above: the parent's textual scope at the `mod` mounting it.
2874
2041
  const inheritedMemo = new Map()
2875
2042
  const inherited = (file, guard = new Set()) => {
2876
2043
  if (inheritedMemo.has(file)) return inheritedMemo.get(file)
@@ -2881,26 +2048,18 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2881
2048
  inheritedMemo.set(file, out)
2882
2049
  return out
2883
2050
  }
2884
- // Crate names resolve per package (a path dep's, a rename) and per the code asking (a build
2885
- // script's tables, the platforms it is compiled for), so per file; the same (name, file) comes up
2886
- // once per reference to the crate, and every glob import's source resolution asks. `{ file,
2887
- // alternatives }`: the crate root, and -- when tables that may each apply name different
2888
- // packages (cargo.js crateCandidates) -- each of them by the table's platform.
2051
+ // Per (name, file), asked once per reference: renames, a build script's tables and platforms differ by file.
2889
2052
  const crates = new Map()
2890
- const crateOf = (name, from) => cached(crates, `${name}\0${from}`, () => {
2053
+ const crateOf = (name, from) => upsert(crates, `${name}\0${from}`, () => {
2891
2054
  const found = ctx ? ctx.crateCandidates(name, from, { roots: files.get(from)?.roots ?? null, units: unitsOf(units, from) }) : []
2892
2055
  const file = found[0]?.file ?? resolveVendoredCrate(name, { knownSources: sources })
2893
2056
  return { file, alternatives: found.length > 1 ? new Map(found.map((c) => [cfgKey(c.key), c.file])) : undefined }
2894
2057
  })
2895
2058
  const resolveCrate = (name, from) => crateOf(name, from).file
2896
2059
  const crateAlternatives = (name, from) => crateOf(name, from).alternatives
2897
- // The crates a crate root's `#[macro_use] extern crate`s bring the exported macros of into every
2898
- // module's scope, as their roots.
2899
- const macroUse = new Map()
2900
- const macroUseCrates = (root) => cached(macroUse, root, () => (scanned.get(root)?.externCrates ?? []).filter((e) => e.macroUse && !NON_CRATE_LEADS.has(e.name)).map((e) => resolveCrate(e.name, root)).filter(Boolean))
2901
- // One context for every path of every file: `provided` memoizes across them (see walkPath).
2902
- // Per crate root, name → the modules with anything of the name (a child module, an item, an
2903
- // import binding it; the root for an exported macro): what hasAll may answer for.
2060
+ // The crates whose exported macros a root's `#[macro_use] extern crate`s bring into every module's scope.
2061
+ const macroUseCrates = memoized((root) => (scanned.get(root)?.externCrates ?? []).filter((e) => e.macroUse && !NON_CRATE_LEADS.has(e.name)).map((e) => resolveCrate(e.name, root)).filter(Boolean))
2062
+ // Per crate root, name → the modules with anything of the name: what hasAll may answer for.
2904
2063
  const having = new Map()
2905
2064
  for (const [root, byParent] of children) for (const [parent, names] of byParent) for (const name of names) note(having, root, name, parent)
2906
2065
  for (const [root, byModule] of defined) for (const [module, names] of byModule) for (const name of names.keys()) note(having, root, name, module)
@@ -2908,12 +2067,11 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2908
2067
  for (const [root, byName] of macros) for (const name of byName.keys()) note(having, root, name, 'crate')
2909
2068
  const declaresCrate = (name, from) => ctx?.declaresCrate?.(name, from) === true
2910
2069
  const pathCtx = { trees, children, moduleFiles, files, resolveCrate, crateAlternatives, declaresCrate, imports, defined, modScope, macros, exportedMacros, fileMacros, externPrelude, leafSets, having, provided: new Map(), closures: new Map(), opaque: new Map(), opaqueGlobs: new Map(), walking: 0, hits: new Set() }
2911
- // Every path of every file, on top of the `mod` / include edges (`resolutions`): once, and once
2912
- // more when the first pass left glob closures short of an import cycle (stabilizeClosures).
2070
+ // Resolved once more when the first pass left glob closures short of an import cycle.
2913
2071
  const resolveAll = () => {
2914
2072
  const out = new Map()
2915
2073
  const unresolved = new Set()
2916
- const wanted = new Map() // root -> the units it is wanted as
2074
+ const wanted = new Map()
2917
2075
  for (const [path, items] of scanned) {
2918
2076
  const specMap = new Map(resolutions.get(path))
2919
2077
  out.set(path, specMap)
@@ -2922,9 +2080,7 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2922
2080
  return { out, unresolved, wanted }
2923
2081
  }
2924
2082
  const resolveFile = (path, items, specMap, unresolved, wanted) => {
2925
- // Only edges to bundled files, never to self, first spelling wins. A target with
2926
- // `alternatives` (withAlternatives) is recorded as the cfg-keyed map of them -- as a `mod`
2927
- // with cfg variants is -- less the asking file and files not bundled; one file left is a plain edge.
2083
+ // Only edges to bundled files, never to self, first spelling wins; alternatives as a cfg-keyed map.
2928
2084
  const add = (spec, target, alternatives) => {
2929
2085
  if (specMap.has(spec)) return
2930
2086
  if (alternatives !== undefined) {
@@ -2938,21 +2094,18 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2938
2094
  }
2939
2095
  if (target && target !== path && sources.has(target)) specMap.set(spec, target)
2940
2096
  }
2941
- // A crate resolved to a root the walk didn't load (its name was bound in the walking file as
2942
- // a value, see resolvePathRef), or loaded as fewer units than this file names it as: the
2943
- // caller may load it and build again.
2097
+ // A root the walk didn't load, or loaded as fewer units than this file names it as: the caller may load it again.
2944
2098
  const addCrate = (name, target, alternatives) => {
2945
2099
  for (const file of alternatives === undefined ? [target] : alternatives.values()) {
2946
2100
  if (file && ctx) {
2947
2101
  const want = new Set([...unitsOf(units, path)].map((u) => ctx.unitOfCrate(file, u)))
2948
2102
  const have = sources.has(file) ? unitsOf(units, file) : new Set()
2949
- if (!want.isSubsetOf(have)) for (const u of want) (wanted.get(file) ?? wanted.set(file, new Set()).get(file)).add(u)
2103
+ if (!want.isSubsetOf(have)) for (const u of want) upsert(wanted, file, () => new Set()).add(u)
2950
2104
  } else if (file && !sources.has(file)) wanted.set(file, DEFAULT_UNITS)
2951
2105
  }
2952
2106
  add(`use ${name}`, target, alternatives)
2953
2107
  }
2954
- // A crate name nothing in-tree satisfied -- sysroot crates aside, and an uppercase lead is an
2955
- // `Enum::Variant` import, not a crate.
2108
+ // An uppercase lead is an `Enum::Variant` import, not a crate.
2956
2109
  const noteUnresolved = (name) => {
2957
2110
  if (!OTHER_SYSROOT_CRATES.has(name) && /^[a-z_]/u.test(name)) unresolved.add(name)
2958
2111
  }
@@ -2966,49 +2119,32 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
2966
2119
  if (r.macro !== undefined) add(`${ref.spec}!`, r.macro.file, r.macro.alternatives)
2967
2120
  }
2968
2121
  else if (r.kind === 'unresolved') {
2969
- // Only a `use` reliably says its lead names a crate (see scanRustItems' `fromUse`) -- or
2970
- // the package declaring a dependency of that name (`serde_json::to_string(…)`), which it
2971
- // then is.
2122
+ // Only a `use`, or the package declaring a dependency of that name, reliably says a lead names a crate.
2972
2123
  if (ref.fromUse || declaresCrate(r.name, path)) noteUnresolved(r.name)
2973
2124
  continue
2974
2125
  } else add(ref.macroCall ? `${ref.spec}!` : ref.spec, r.file, r.alternatives) // `crate::helper!()` beside `crate::helper()`: two edges
2975
- // The module an import was followed from, when the path's own segments name it (`super`,
2976
- // `crate::util`) and its file is neither this one nor the crate root, which every file of
2977
- // the crate hangs off anyway.
2126
+ // Not the crate root, which every file of the crate hangs off anyway.
2978
2127
  const v = r.via
2979
2128
  if (v && v.consumed > 0 && v.through !== path && v.through !== here.root && v.through !== r.file && !via.has(v.through)) {
2980
2129
  via.set(v.through, ref.segments.slice(0, v.consumed).join('::'))
2981
2130
  }
2982
2131
  }
2983
2132
  for (const { name } of items.externCrates) {
2984
- if (NON_CRATE_LEADS.has(name)) continue // `extern crate self as x;` / `extern crate alloc;`
2133
+ if (NON_CRATE_LEADS.has(name)) continue
2985
2134
  const { file: target, alternatives } = crateOf(name, path)
2986
2135
  if (target) addCrate(name, target, alternatives)
2987
2136
  else noteUnresolved(name)
2988
2137
  }
2989
- // A macro invoked by bare name and defined by `macro_rules!` in textual scope (see mounts):
2990
- // this file's own, what the files above hand down (bytes' `fmt_impl!`, from fmt/mod.rs in
2991
- // fmt/debug.rs), what a `#[macro_use] mod` of this file brings in (tokio's `ready!`, from
2992
- // macros/ready.rs), and -- in the crate root's file, whose items they are -- the exported ones;
2993
- // else one the module's imports bring in by path (`use crate::combinator::dispatch;`, a `use
2994
- // super::*` from the root). (A `macro_rules! vec` in some sibling file of the crate is not
2995
- // what `vec!` names.)
2138
+ // A bare `name!`: one in textual scope, else one the imports bring in (a sibling's `macro_rules! vec` isn't `vec!`).
2996
2139
  if (here !== undefined) {
2997
2140
  const above = inherited(path)
2998
2141
  const exported = here.modulePath === 'crate' ? macros.get(here.root) : undefined
2999
- // The macro a bare call of `name` at `offset` names, `{ file, name }` (the file defining it,
3000
- // by the name it defines it under): one in textual scope; else one an import brings in --
3001
- // a file that defines a macro of that name, not a `fn write` beside `write!` -- or a
3002
- // `#[macro_use] extern crate` of the crate root does.
3003
2142
  const macroFileAt = (name, offset) => {
3004
2143
  const inScope = macroAt(path, name, offset, offset, above) ?? exported?.get(name)
3005
2144
  if (inScope !== undefined) return { file: inScope, name }
3006
2145
  const byPath = walkPath([name], here.root, here.modulePath, pathCtx, { ns: 'macro', file: path, asker: here.asker })
3007
2146
  const as = byPath?.macroName ?? name
3008
- if (byPath?.file !== undefined && fileMacros.get(byPath.file)?.has(as) === true) {
3009
- const alternatives = byPath.alternatives === undefined ? undefined : new Map([...byPath.alternatives].filter(([, f]) => fileMacros.get(f)?.has(as) === true))
3010
- return { file: byPath.file, name: as, alternatives: alternatives?.size > 1 ? alternatives : undefined }
3011
- }
2147
+ if (byPath?.file !== undefined && definesMacro(pathCtx, byPath.file, as)) return { file: byPath.file, name: as, alternatives: macroAlternatives(pathCtx, byPath.alternatives, as) }
3012
2148
  for (const crate of macroUseCrates(here.root)) {
3013
2149
  const macro = macroOfCrate(crate, [name], pathCtx)
3014
2150
  if (macro !== null) return { file: macro.file, name: macro.macroName, alternatives: macro.alternatives }
@@ -3016,11 +2152,8 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
3016
2152
  return undefined
3017
2153
  }
3018
2154
  for (const [name, offsets] of items.calls) {
3019
- // Scope is decided at each call: a `macro_rules!` later in the file doesn't shadow what an
3020
- // earlier `#[macro_use] mod` handed an earlier call. The bare calls in the invoked
3021
- // macro's template, and in theirs on down, are made here too -- resolved in this file's
3022
- // scope at this call, as rustc expands them.
3023
- const found = new Map() // `name!\0file` -> the cfg-keyed files, when several may define it
2155
+ // Scope is decided per call; the bare calls in the invoked macro's template are made here too.
2156
+ const found = new Map()
3024
2157
  for (const offset of offsets) {
3025
2158
  const seen = new Set()
3026
2159
  const expand = (called) => {
@@ -3028,22 +2161,16 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
3028
2161
  seen.add(called)
3029
2162
  const macro = macroFileAt(called, offset)
3030
2163
  if (macro === undefined) return
3031
- found.set(`${called}!\0${macro.file}`, macro.alternatives)
2164
+ found.set(`${called}!\0${macro.file}`, [`${called}!`, macro.file, macro.alternatives])
3032
2165
  for (const m of scanned.get(macro.file)?.macros ?? []) if (m.name === macro.name) for (const inner of m.calls) expand(inner)
3033
2166
  }
3034
2167
  expand(name)
3035
2168
  }
3036
- for (const [edge, alternatives] of found) {
3037
- const cut = edge.indexOf('\0')
3038
- add(edge.slice(0, cut), edge.slice(cut + 1), alternatives)
3039
- }
2169
+ for (const [spec, file, alternatives] of found.values()) add(spec, file, alternatives)
3040
2170
  }
3041
2171
  }
3042
- // A module whose import a path went through is a dependency of the file too, whatever the
3043
- // import led to (bitflags' `$crate::__private::serde::Serialize` needs the root's `__private`
3044
- // as much as serde). When nothing else in the file points at that module's file, the path's
3045
- // prefix naming the module records it (`crate::__private` → lib.rs); the bare crate root is
3046
- // implied by the module tree.
2172
+ // A module whose import a path went through is a dependency of the file too (bitflags'
2173
+ // `$crate::__private::serde::Serialize` needs the root's `__private`), recorded when nothing else points at it.
3047
2174
  const targets = new Set()
3048
2175
  for (const t of specMap.values()) for (const f of t instanceof Map ? t.values() : [t]) targets.add(f)
3049
2176
  for (const [owner, spec] of via) if (!targets.has(owner)) add(spec, owner)
@@ -3053,61 +2180,38 @@ export function buildRustTree(sources, { roots = [], baseDir = null, cargo = nul
3053
2180
  return { sources, resolutions: out, missing, unresolvedCrates: unresolved, wantedRoots: [...wanted.keys()], wantedUnits: wanted }
3054
2181
  }
3055
2182
 
3056
- // --- Disk walk --------------------------------------------------------------------------
3057
-
3058
- // Walk from `entries` (the crate roots), following `mod` declarations and references to in-tree
3059
- // crates, reading each reachable file once; a missing file warns and is skipped without aborting
3060
- // the walk (buildRustTree decides what is fatal). `cargo` (cargo.js) resolves crate names and
3061
- // carries each crate's features; one is created for `entries` when not passed in. Pass `sources`
3062
- // to walk further roots into an earlier walk's map (files already there are not read again, but
3063
- // are gone through again when they are now compiled as more units). `units`: file → the compile
3064
- // units it is compiled as (cargo.js unitKey), filled in as the walk goes -- an entry the map
3065
- // doesn't name is the target's; what a file declares or includes is compiled as that file is,
3066
- // and a crate it names as the Cargo context says (unitOfCrate: a proc-macro crate, or a crate
3067
- // named from code built for the host, is built for the host). `crateRoots`: file → the crate roots
3068
- // it is compiled in, filled in the same way (an entry is its own; a build script's modules are the
3069
- // script's, and name the crates of its `[build-dependencies]`). `unloaded` gets the files the walk
3070
- // refused (a link out of their package) or found gone, each warned about.
2183
+ // Reads each file once into `sources`, an earlier walk's too: a file now compiled as more units is gone through again.
3071
2184
  export async function collectRustFilesFromDisk(baseDir, entries, { cargo = null, formats = null, sources = new Map(), units = new Map(), crateRoots: rootsOf = new Map(), unloaded = new Set() } = {}) {
3072
2185
  const realBase = realpathSync(baseDir)
3073
2186
  const roots = new Set(entries)
3074
- const pathLoaded = new Set() // files a `#[path]` named: their submodules sit beside them
3075
- const assets = new Map() // files an `include_str!` / `include_bytes!` names: carried as they are, not scanned
3076
- const rustFiles = new Set(entries) // files named as Rust source (a `mod`, an `include!`): scanned, never an asset
2187
+ const pathLoaded = new Set()
2188
+ const assets = new Map() // carried as they are, not scanned
2189
+ const rustFiles = new Set(entries) // never an asset
3077
2190
  const ctx = cargo ?? createCargoContext(baseDir, { entries })
3078
2191
  const modOpts = { baseDir, roots, pathLoaded }
3079
- // file → the directory it must really lie in (boundaryOf, from the file that named it): a
3080
- // vendored crate's files stay in its package, symlinks included.
2192
+ // file → the directory it must really lie in: a vendored crate's files stay in its package, symlinks included.
3081
2193
  const boundaries = new Map(entries.map((e) => [e, boundaryOf(e, ctx)]))
3082
- const realDirs = new Map() // those directories' real paths
3083
- // An include macro in a `macro_rules!` body names a file relative to each file invoking the
3084
- // macro -- through the templates that call it, the outermost (effectiveInvokers): definitions
3085
- // and invocations arrive in any order, so each side is kept and paired. Every definition of a
3086
- // name counts (two `macro_rules! m` in two files: both bodies' assets).
3087
- const templateIncludes = new Map() // macro name → its bodies' includes
3088
- const templateMods = new Map() // macro name → its bodies' `mod`s, each `{ decl, from }` (see buildRustTree)
3089
- const templateCalls = new Map() // macro name → the names its bodies call by bare name
3090
- const invokers = new Map() // macro name → the files invoking it, by bare name or by path
3091
- const scansOf = new Map() // file → its scan, for where it invokes what (hostedCopies)
3092
- // A package defining a `macro_rules!` of a template macro's name (its own `quote!`) invokes that
3093
- // one everywhere in the package (see buildRustTree): its files are scanned without the name in
3094
- // the template set -- again, for the ones scanned before the definition was met.
2194
+ const realDirs = new Map()
2195
+ // Template definitions and invocations arrive in any order, so each side is kept and paired.
2196
+ const templateIncludes = new Map()
2197
+ const templateMods = new Map()
2198
+ const templateCalls = new Map()
2199
+ const invokers = new Map()
2200
+ const scansOf = new Map()
2201
+ // A package defining its own `quote!` has its files scanned again without the name (see buildRustTree).
3095
2202
  const packageOf = (rel) => ctx.packageInfo(rel)?.dir ?? '.'
3096
- const ownTemplates = new Map() // package → the template names it defines
3097
- const skippedIn = new Map() // package → its files that skipped a template body
2203
+ const ownTemplates = new Map()
2204
+ const skippedIn = new Map()
3098
2205
  const scanFor = (relPath, content) => {
3099
2206
  const own = ownTemplates.get(packageOf(relPath))
3100
2207
  return cachedScan(sources, relPath, content, buildOf(relPath, ctx, units), own === undefined ? undefined : templatesLess(own))
3101
2208
  }
3102
2209
  withEntryUnits(units, entries, ctx)
3103
2210
  for (const e of entries) addTo(rootsOf, e, new Set([e]))
3104
- // Files already walked that are now compiled as more units, or in more crates: gone through
3105
- // again (their build changed, and so may what they name, and as what).
2211
+ // Files walked before and now compiled as more units, or in more crates: what they name may change.
3106
2212
  const revisit = new Set(entries.filter((e) => sources.has(e) && !formats?.has(e)))
3107
2213
 
3108
- // What a Rust file names, queued for the next wave: compiled as the naming file is -- as its
3109
- // units, a crate as unitOfCrate says (`as`); in its crates, or the crate it is the root of
3110
- // (`crate`) -- and, for a vendored crate's, within its package (`within`).
2214
+ // Compiled as the naming file is (its units, its crates) unless `as`/`crate` say otherwise.
3111
2215
  let next = []
3112
2216
  const queue = (from, file, { rust, within = boundaryOf(from, ctx), as = unitsOf(units, from), crate = null }) => {
3113
2217
  if (rust) {
@@ -3124,8 +2228,7 @@ export async function collectRustFilesFromDisk(baseDir, entries, { cargo = null,
3124
2228
  if (!sources.has(file)) next.push(file)
3125
2229
  else if (grew && rust && !formats?.has(file)) revisit.add(file)
3126
2230
  }
3127
- // An include macro's file, relative to `from` (the file it is written in, or one invoking the
3128
- // macro whose body it is in).
2231
+ // `from`: the file an include or `mod` is written in, or one invoking the macro whose body it is in.
3129
2232
  const includeFrom = (inc, from) => {
3130
2233
  const file = includeTarget(inc, from, ctx)
3131
2234
  if (file === null || file === from) return
@@ -3139,34 +2242,30 @@ export async function collectRustFilesFromDisk(baseDir, entries, { cargo = null,
3139
2242
  queue(from, file, { rust: false })
3140
2243
  }
3141
2244
  }
3142
- // A `mod` declaration's files, relative to `from` (the file it is written in, or one invoking
3143
- // the macro whose body it is in).
3144
2245
  const modFrom = (decl, from) => {
3145
2246
  for (const { file, explicit } of resolveModDecl(decl, from, { ...modOpts, boundary: boundaryOf(from, ctx) })) {
3146
2247
  if (explicit) pathLoaded.add(file)
3147
2248
  queue(from, file, { rust: true })
3148
2249
  }
3149
2250
  }
3150
- // The include macros of every template, relative to every file in effect invoking it; its
3151
- // `mod`s relative to those of its own package.
2251
+ const hostsOfMod = (name, { from, exported }) => hostsOf(name, from, invokers, templateCalls, exported ? null : packageOf)
3152
2252
  const expandTemplates = () => {
3153
2253
  for (const [name, incs] of templateIncludes) {
3154
2254
  for (const f of effectiveInvokers(name, invokers, templateCalls)) for (const inc of incs) includeFrom(inc, f)
3155
2255
  }
3156
2256
  for (const [name, decls] of templateMods) {
3157
- for (const { decl, from, exported } of decls.values()) for (const f of hostsOf(name, from, invokers, templateCalls, exported ? null : packageOf)) for (const copy of hostedCopies(decl, scansOf.get(f), templateCalls)) modFrom(copy, f)
2257
+ for (const t of decls.values()) for (const f of hostsOfMod(name, t)) for (const copy of hostedCopies(t.decl, scansOf.get(f), templateCalls)) modFrom(copy, f)
3158
2258
  }
3159
2259
  }
3160
2260
 
3161
- // A template's `mod`s that nothing in its package invokes by bare name stand beside its
3162
- // definition (see buildRustTree): walked once nothing else is left to, and what they name after.
2261
+ // A template's `mod`s nothing in its package invokes stand beside its definition, walked once nothing else is left.
3163
2262
  const placed = new Set()
3164
2263
  const placeUninvoked = () => {
3165
2264
  for (const [name, decls] of templateMods) {
3166
- for (const { decl, from, exported } of decls.values()) {
3167
- if (placed.has(decl) || hostsOf(name, from, invokers, templateCalls, exported ? null : packageOf).length > 0) continue
3168
- placed.add(decl)
3169
- modFrom(decl, from)
2265
+ for (const t of decls.values()) {
2266
+ if (placed.has(t.decl) || hostsOfMod(name, t).length > 0) continue
2267
+ placed.add(t.decl)
2268
+ modFrom(t.decl, t.from)
3170
2269
  }
3171
2270
  }
3172
2271
  }
@@ -3182,9 +2281,8 @@ export async function collectRustFilesFromDisk(baseDir, entries, { cargo = null,
3182
2281
  unloaded.add(relPath)
3183
2282
  return null
3184
2283
  }
3185
- // Rust source (a module, a build script, an `include!`d file) and an `include_str!` asset
3186
- // are UTF-8 text, or rustc rejects them: one that isn't is refused, never carried with
3187
- // its bytes replaced. An `include_bytes!` asset is any bytes, carried as base64 if need be.
2284
+ // Rust source and an `include_str!` asset are UTF-8 or rustc rejects them: one that isn't is refused,
2285
+ // never carried with its bytes replaced. An `include_bytes!` asset is any bytes, base64 if need be.
3188
2286
  const kind = assets.get(relPath)
3189
2287
  const buf = await readFile(join(baseDir, relPath))
3190
2288
  ctx.checkVendoredFile?.(relPath, buf)
@@ -3209,48 +2307,37 @@ export async function collectRustFilesFromDisk(baseDir, entries, { cargo = null,
3209
2307
  })
3210
2308
  )
3211
2309
  next = []
3212
- // What a Rust file names, queued (see queue). Run again for a file whose package turned out
3213
- // to define a template macro (below): the file's `quote!` bodies are its own then.
2310
+ // Run again for a file whose package turned out to define a template macro: its `quote!` bodies are its own then.
3214
2311
  const handle = (relPath, content) => {
3215
2312
  const here = unitsOf(units, relPath)
3216
2313
  const items = scanFor(relPath, content)
3217
2314
  const { mods, refs, externCrates, bindings, includes, macros } = items
3218
2315
  const pkg = packageOf(relPath)
3219
- if (items.skipped.size > 0) (skippedIn.get(pkg) ?? skippedIn.set(pkg, new Set()).get(pkg)).add(relPath)
2316
+ if (items.skipped.size > 0) upsert(skippedIn, pkg, () => new Set()).add(relPath)
3220
2317
  for (const m of macros) {
3221
2318
  if (!TEMPLATE_MACROS.has(m.name)) continue
3222
- const own = ownTemplates.get(pkg) ?? ownTemplates.set(pkg, new Set()).get(pkg)
2319
+ const own = upsert(ownTemplates, pkg, () => new Set())
3223
2320
  if (own.has(m.name)) continue
3224
2321
  own.add(m.name)
3225
2322
  for (const f of skippedIn.get(pkg) ?? []) if (f !== relPath) handle(f, sources.get(f))
3226
2323
  }
3227
- // An `include!`d file's `mod` declarations resolve beside that file (it is in `pathLoaded`
3228
- // from when it was queued), within its package.
3229
- // A template's `mod`s wait for the files invoking it (expandTemplates).
2324
+ // A template's `mod`s and includes wait for the files invoking it (expandTemplates).
3230
2325
  for (const decl of mods) {
3231
2326
  if (decl.template === null) modFrom(decl, relPath)
3232
2327
  else nestedMap(templateMods, decl.template).set(`${relPath}\0${decl.at}`, { decl, from: relPath, exported: macros.some((m) => m.name === decl.template && m.exported) })
3233
2328
  }
3234
- // `include!("x.rs")` splices Rust source (scanned next wave, under this module); the asset
3235
- // macros name a file to carry as it is. A build-output path (`concat!(env!("OUT_DIR"), …)`)
3236
- // never gets here. A template's include macros are resolved once the wave is in
3237
- // (expandTemplates), against the files invoking it.
3238
2329
  for (const inc of includes) includeFrom(inc, relPath)
3239
2330
  for (const m of macros) {
3240
2331
  if (m.includes.length === 0) continue
3241
- const list = templateIncludes.get(m.name) ?? templateIncludes.set(m.name, []).get(m.name)
2332
+ const list = upsert(templateIncludes, m.name, () => [])
3242
2333
  for (const inc of m.includes) if (!list.some((x) => x.kind === inc.kind && x.path === inc.path && x.base === inc.base)) list.push(inc)
3243
2334
  }
3244
2335
  noteMacroCalls(invokers, templateCalls, relPath, items)
3245
2336
  scansOf.set(relPath, items)
3246
- // A reference to an in-tree crate pulls its root in (a crate root by role, whatever its
3247
- // name -- `[lib] path` may point anywhere); the root's own `mod` edges follow next wave.
2337
+ // A reference to an in-tree crate pulls its root in, a crate root by role whatever its name (`[lib] path`).
3248
2338
  const leads = new Set()
3249
2339
  for (const e of externCrates) if (!NON_CRATE_LEADS.has(e.name)) leads.add(e.name)
3250
- for (const r of refs) {
3251
- const name = crateLead(r, bindings)
3252
- if (name !== null) leads.add(name)
3253
- }
2340
+ for (const name of refs.map((r) => crateLead(r, bindings))) if (name !== null) leads.add(name)
3254
2341
  for (const name of leads) {
3255
2342
  for (const { file: lib } of ctx.crateCandidates(name, relPath, { roots: rootsOf.get(relPath) ?? null, units: here })) {
3256
2343
  roots.add(lib)
@@ -3272,30 +2359,20 @@ export async function collectRustFilesFromDisk(baseDir, entries, { cargo = null,
3272
2359
  for (const relPath of again) handle(relPath, sources.get(relPath))
3273
2360
  }
3274
2361
  if (next.length === 0) placeUninvoked()
3275
- const following = next
3276
- await processWave(following)
2362
+ await processWave(next)
3277
2363
  }
3278
2364
 
3279
2365
  await processWave(entries)
3280
2366
  return sources
3281
2367
  }
3282
2368
 
3283
- // The files of a Rust bundle of `entries`, from disk, and its tree: the walk
3284
- // (collectRustFilesFromDisk) and -- with `buildScripts` -- the build script of every package it
3285
- // reaches, each a crate root of its own, until no package is new; then the tree pass
3286
- // (buildRustTree), again with the in-tree crate roots it names that the walk left out (a crate
3287
- // whose name a file also binds as a value, `use crate::util::log;` beside `log::info!`) or walked
3288
- // as fewer units than it is named as (`wantedRoots`), until none is new. Each file is compiled as
3289
- // the units `units` records (cargo.js unitKey): a build script for the host, with its package's
3290
- // features, and so what it reaches; a proc-macro crate and what it depends on for the host too.
3291
- // A root the walk was asked for and didn't load -- refused (a link out of its package) or gone --
3292
- // is not asked for again: the walk has said why. Returns `{ sources, formats, tree }`.
2369
+ // Walks again the roots the tree pass wants until none is new; one refused or gone isn't asked for again.
3293
2370
  export async function collectRustBundle(baseDir, entries, { cargo, buildScripts = false }) {
3294
2371
  const formats = new Map()
3295
2372
  const sources = new Map()
3296
2373
  const units = new Map()
3297
2374
  const rootsOf = new Map()
3298
- const unloaded = new Set() // what the walk refused or found gone: said once
2375
+ const unloaded = new Set()
3299
2376
  const roots = [...entries]
3300
2377
  const walk = async (wave) => {
3301
2378
  await collectRustFilesFromDisk(baseDir, wave, { cargo, formats, sources, units, crateRoots: rootsOf, unloaded })