nu-protocol 0.117.0

Nushell's internal protocols, including its abstract syntax tree
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use crate::{DeclId, ModuleId, OverlayId, Span, TypeAliasId, VarId};
use rustc_hash::FxBuildHasher;
use std::{
    collections::HashMap,
    ops::Deref,
    sync::atomic::{AtomicUsize, Ordering},
};

/// The longest name ever inserted into any [`DeclNameMap`] in this process.
static LONGEST_DECL_NAME: AtomicUsize = AtomicUsize::new(0);

/// An upper bound on the length of every declaration name: every name any [`DeclNameMap`] holds,
/// in any engine state, is at most this long, so no lookup of a longer name can succeed.
///
/// `find_decl` searches only [`DeclNameMap`]s (declarations and predeclarations, in every scope
/// and overlay), so the parser uses this to skip building command-name candidates that cannot
/// match (see `find_longest_decl_with_prefix` in nu-parser). The bound is shared by every engine
/// in the process and only grows, so a long name declared anywhere, even in a scope that is gone,
/// makes it less tight; that only lets the parser build longer candidates, as it did without it.
pub fn longest_decl_name() -> usize {
    LONGEST_DECL_NAME.load(Ordering::Relaxed)
}

/// Name → id map for declarations that remembers the longest name it has ever held.
///
/// Command resolution tries the longest possible command name first and shortens it a word at a
/// time (`find_longest_decl_with_prefix` in nu-parser), looking each candidate up in every map on
/// the scope chain. It only builds candidates up to [`longest_decl_name`], the longest name in any
/// map, but most maps hold much shorter names, such as a script's own definitions or a module's.
/// Knowing the longest name it holds lets [`DeclNameMap::get`] reject a longer candidate before
/// hashing it. The bound only grows (removals leave it alone), so it is always an upper bound on
/// the keys present.
///
/// Reads go through `Deref` to the underlying `HashMap`; all mutation goes through the inherent
/// methods so the bound stays valid.
///
/// The parser looks names up here for every command word it sees, so the map uses the Fx hash
/// (a multiply per 8 bytes) rather than SipHash. Nothing depends on the order of its entries.
/// Unlike SipHash, the Fx hash has no per-process key, so a file could declare names chosen to
/// collide and make its own parse slow, in the LSP or `nu-check` as much as when it runs. That is
/// accepted for the speed, as rustc does, since the names come from the code being parsed.
#[derive(Debug, Clone, Default)]
pub struct DeclNameMap {
    map: HashMap<Vec<u8>, DeclId, FxBuildHasher>,
    longest_name: usize,
}

impl DeclNameMap {
    pub fn new() -> Self {
        Self::default()
    }

    /// Look up a declaration by name; names longer than any key ever inserted are rejected
    /// without hashing.
    pub fn get(&self, name: &[u8]) -> Option<&DeclId> {
        if name.len() > self.longest_name {
            return None;
        }
        self.map.get(name)
    }

    pub fn insert(&mut self, name: Vec<u8>, decl_id: DeclId) -> Option<DeclId> {
        self.longest_name = self.longest_name.max(name.len());
        LONGEST_DECL_NAME.fetch_max(name.len(), Ordering::Relaxed);
        self.map.insert(name, decl_id)
    }

    pub fn remove(&mut self, name: &[u8]) -> Option<DeclId> {
        self.map.remove(name)
    }

    pub fn remove_entry(&mut self, name: &[u8]) -> Option<(Vec<u8>, DeclId)> {
        self.map.remove_entry(name)
    }
}

impl Deref for DeclNameMap {
    type Target = HashMap<Vec<u8>, DeclId, FxBuildHasher>;

    fn deref(&self) -> &Self::Target {
        &self.map
    }
}

impl<'a> IntoIterator for &'a DeclNameMap {
    type Item = (&'a Vec<u8>, &'a DeclId);
    type IntoIter = std::collections::hash_map::Iter<'a, Vec<u8>, DeclId>;

    fn into_iter(self) -> Self::IntoIter {
        self.map.iter()
    }
}

impl IntoIterator for DeclNameMap {
    type Item = (Vec<u8>, DeclId);
    type IntoIter = std::collections::hash_map::IntoIter<Vec<u8>, DeclId>;

    fn into_iter(self) -> Self::IntoIter {
        self.map.into_iter()
    }
}

impl Extend<(Vec<u8>, DeclId)> for DeclNameMap {
    fn extend<I: IntoIterator<Item = (Vec<u8>, DeclId)>>(&mut self, iter: I) {
        for (name, decl_id) in iter {
            self.insert(name, decl_id);
        }
    }
}

impl FromIterator<(Vec<u8>, DeclId)> for DeclNameMap {
    fn from_iter<I: IntoIterator<Item = (Vec<u8>, DeclId)>>(iter: I) -> Self {
        let mut map = Self::default();
        map.extend(iter);
        map
    }
}

pub static DEFAULT_OVERLAY_NAME: &str = "zero";

/// Tells whether a decl is visible or not
///
/// Looked up for every declaration a name lookup finds (see [`VisibilityStack`]), so it uses the
/// Fx hash like [`DeclNameMap`].
#[derive(Debug, Clone)]
pub struct Visibility {
    decl_ids: HashMap<DeclId, bool, FxBuildHasher>,
    /// Type aliases hidden with `hide`, or imported again after that, kept like `decl_ids`.
    type_alias_ids: HashMap<TypeAliasId, bool, FxBuildHasher>,
}

/// The names an unscoped parse of a file bound in the scope frame it ran in: the commands,
/// modules, variables and type aliases the file declared or imported there.
///
/// An unscoped parse (`source`, a script, a REPL entry) declares a file's names in the caller's
/// scope frame. The parser's block cache keeps this record on the parsed
/// [`Block`](crate::ast::Block). Reusing the block for a later parse of the same file declares
/// nothing by itself, so the cache binds these names again wherever they are missing, such as
/// after the scope of the first parse has ended (see
/// [`StateWorkingSet::bind_names`](crate::engine::StateWorkingSet::bind_names)).
#[derive(Debug, Clone, Default)]
pub struct BoundNames {
    pub decls: Vec<(Vec<u8>, DeclId)>,
    pub modules: Vec<(Vec<u8>, ModuleId)>,
    pub vars: Vec<(Vec<u8>, VarId)>,
    pub type_aliases: Vec<(Vec<u8>, TypeAliasId)>,
}

/// Name bindings introduced while parsing a single block/closure scope.
///
/// Nested scopes discard their name maps on `exit_scope`; this snapshot is stored on the
/// [`Block`](crate::ast::Block) so `scope` commands can report locals at runtime.
///
/// # Lifecycle
///
/// 1. **Parse**: [`StateWorkingSet::snapshot_scope_bindings`] copies decls/modules from the
///    innermost scope frame into a `ScopeBindings` attached to the block, immediately before
///    the matching `exit_scope`.
/// 2. **Eval**: whole blocks push bindings on [`Stack::active_scope_bindings`] in
///    `eval_ir_block`. Keyword bodies that are IR-inlined record
///    [`ScopeRegion`](crate::ir::ScopeRegion)s on the parent [`IrBlock`](crate::ir::IrBlock);
///    `scope` matches the current instruction index against those regions.
#[derive(Debug, Clone, Default)]
pub struct ScopeBindings {
    pub decls: HashMap<Vec<u8>, DeclId>,
    pub modules: HashMap<Vec<u8>, ModuleId>,
    pub visibility: Visibility,
}

impl ScopeBindings {
    pub fn is_empty(&self) -> bool {
        self.decls.is_empty() && self.modules.is_empty() && self.visibility.decl_ids.is_empty()
    }

    /// Merge decls, modules, and visibility from an overlay frame (other wins on name clash).
    pub fn extend_from_overlay(&mut self, overlay: &OverlayFrame) {
        self.decls
            .extend(overlay.decls.iter().map(|(k, v)| (k.clone(), *v)));
        self.modules
            .extend(overlay.modules.iter().map(|(k, v)| (k.clone(), *v)));
        self.visibility.merge_with(overlay.visibility.clone());
    }

    /// Merge another bindings map on top of this one (other wins on name clash).
    pub fn extend_from_bindings(&mut self, other: &ScopeBindings) {
        self.decls
            .extend(other.decls.iter().map(|(k, v)| (k.clone(), *v)));
        self.modules
            .extend(other.modules.iter().map(|(k, v)| (k.clone(), *v)));
        self.visibility.merge_with(other.visibility.clone());
    }
}

impl Visibility {
    pub fn new() -> Self {
        Visibility {
            decl_ids: HashMap::default(),
            type_alias_ids: HashMap::default(),
        }
    }

    pub fn is_decl_id_visible(&self, decl_id: &DeclId) -> bool {
        *self.decl_ids.get(decl_id).unwrap_or(&true) // by default it's visible
    }

    pub fn hide_decl_id(&mut self, decl_id: &DeclId) {
        self.decl_ids.insert(*decl_id, false);
    }

    pub fn use_decl_id(&mut self, decl_id: &DeclId) {
        self.decl_ids.insert(*decl_id, true);
    }

    pub fn is_type_alias_id_visible(&self, type_alias_id: &TypeAliasId) -> bool {
        *self.type_alias_ids.get(type_alias_id).unwrap_or(&true) // by default it's visible
    }

    pub fn hide_type_alias_id(&mut self, type_alias_id: &TypeAliasId) {
        self.type_alias_ids.insert(*type_alias_id, false);
    }

    pub fn use_type_alias_id(&mut self, type_alias_id: &TypeAliasId) {
        self.type_alias_ids.insert(*type_alias_id, true);
    }

    /// Overwrite own values with the other
    pub fn merge_with(&mut self, other: Visibility) {
        self.decl_ids.extend(other.decl_ids);
        self.type_alias_ids.extend(other.type_alias_ids);
    }

    /// Take new values from the other but keep own values
    pub fn append(&mut self, other: &Visibility) {
        for (decl_id, visible) in other.decl_ids.iter() {
            if !self.decl_ids.contains_key(decl_id) {
                self.decl_ids.insert(*decl_id, *visible);
            }
        }
        for (type_alias_id, visible) in other.type_alias_ids.iter() {
            self.type_alias_ids
                .entry(*type_alias_id)
                .or_insert(*visible);
        }
    }

    /// Whether this hides a declaration or type alias that `before` didn't hide.
    pub fn hides_more_than(&self, before: &Visibility) -> bool {
        self.decl_ids
            .iter()
            .any(|(id, visible)| !visible && before.decl_ids.get(id) != Some(&false))
            || self
                .type_alias_ids
                .iter()
                .any(|(id, visible)| !visible && before.type_alias_ids.get(id) != Some(&false))
    }
}

/// Decl visibility resolved across the overlay frames walked so far, innermost frame first.
///
/// Name lookups walk the active overlays from the innermost one outwards. A decl is visible
/// unless one of the frames walked so far has an explicit entry hiding it, and the innermost
/// frame with an entry for the decl wins. This borrows each frame's [`Visibility`] instead of
/// merging the maps: merging copied every entry of every frame on every lookup, which made
/// `find_decl` (called for every command word the parser sees) cost as much as the maps were
/// large.
#[derive(Debug, Default)]
pub struct VisibilityStack<'a> {
    /// The first frames pushed, in order. A lookup rarely walks more than a few frames that
    /// hide declarations (once the standard library is loaded, the permanent overlay is one), so
    /// they are kept here rather than in a `Vec`, which would allocate on every lookup.
    inline: [Option<&'a Visibility>; 4],
    /// The frames pushed after `inline` is full, in order.
    spilled: Vec<&'a Visibility>,
}

impl<'a> VisibilityStack<'a> {
    /// Add the visibility of the next (outer) frame. Frames pushed earlier take precedence.
    ///
    /// A frame that hides nothing can never answer a lookup, so it is not recorded; this keeps
    /// the common lookup (no hidden declarations anywhere) free of allocation. A frame that only
    /// hides type aliases is recorded too, so `find_decl` walks it without finding an answer
    /// there; `hide` on a type alias is rare enough that this costs nothing in practice.
    pub fn push(&mut self, visibility: &'a Visibility) {
        if visibility.decl_ids.is_empty() && visibility.type_alias_ids.is_empty() {
            return;
        }
        match self.inline.iter_mut().find(|layer| layer.is_none()) {
            Some(layer) => *layer = Some(visibility),
            None => self.spilled.push(visibility),
        }
    }

    /// Whether `decl_id` is visible given the frames pushed so far.
    pub fn is_decl_id_visible(&self, decl_id: &DeclId) -> bool {
        self.inline
            .iter()
            .map_while(|layer| *layer)
            .chain(self.spilled.iter().copied())
            .find_map(|visibility| visibility.decl_ids.get(decl_id))
            .copied()
            .unwrap_or(true) // by default it's visible
    }

    /// Whether `type_alias_id` is visible given the frames pushed so far.
    pub fn is_type_alias_id_visible(&self, type_alias_id: &TypeAliasId) -> bool {
        self.inline
            .iter()
            .map_while(|layer| *layer)
            .chain(self.spilled.iter().copied())
            .find_map(|visibility| visibility.type_alias_ids.get(type_alias_id))
            .copied()
            .unwrap_or(true) // by default it's visible
    }
}

#[derive(Debug, Clone)]
pub struct ScopeFrame {
    /// List of both active and inactive overlays in this ScopeFrame.
    ///
    /// The order does not have any meaning. Indexed locally (within this ScopeFrame) by
    /// OverlayIds in active_overlays.
    pub overlays: Vec<(Vec<u8>, OverlayFrame)>,

    /// List of currently active overlays.
    ///
    /// Order is significant: The last item points at the last activated overlay.
    pub active_overlays: Vec<OverlayId>,

    /// Removed overlays from previous scope frames / permanent state
    pub removed_overlays: Vec<Vec<u8>>,

    /// temporary storage for predeclarations
    pub predecls: DeclNameMap,

    /// Where each `type` statement of the block being parsed is, by alias name; filled in with
    /// the predeclarations and used only to tell a use before the definition from an unknown type.
    pub type_predecls: HashMap<Vec<u8>, Vec<Span>>,

    /// Variables that `unlet` hid while this frame was parsed (see
    /// [`StateWorkingSet::hide_variable`](crate::engine::StateWorkingSet::hide_variable)). Only
    /// the parser's frames use it: merging a delta removes the variables its top frame hid
    /// from the overlays instead.
    pub hidden_vars: Vec<VarId>,
}

impl ScopeFrame {
    pub fn new() -> Self {
        Self {
            overlays: vec![],
            active_overlays: vec![],
            removed_overlays: vec![],
            predecls: DeclNameMap::new(),
            type_predecls: HashMap::new(),
            hidden_vars: vec![],
        }
    }

    pub fn with_empty_overlay(name: Vec<u8>, origin: ModuleId, prefixed: bool) -> Self {
        Self {
            overlays: vec![(name, OverlayFrame::from_origin(origin, prefixed))],
            active_overlays: vec![OverlayId::new(0)],
            removed_overlays: vec![],
            predecls: DeclNameMap::new(),
            type_predecls: HashMap::new(),
            hidden_vars: vec![],
        }
    }

    pub fn get_var(&self, var_name: &[u8]) -> Option<&VarId> {
        for overlay_id in self.active_overlays.iter().rev() {
            if let Some(var_id) = self
                .overlays
                .get(overlay_id.get())
                .expect("internal error: missing overlay")
                .1
                .vars
                .get(var_name)
            {
                return Some(var_id);
            }
        }

        None
    }

    pub fn active_overlay_ids(&self, removed_overlays: &mut Vec<Vec<u8>>) -> Vec<OverlayId> {
        for name in &self.removed_overlays {
            if !removed_overlays.contains(name) {
                removed_overlays.push(name.clone());
            }
        }

        self.active_overlays
            .iter()
            .filter(|id| {
                !removed_overlays
                    .iter()
                    .any(|name| name == self.get_overlay_name(**id))
            })
            .copied()
            .collect()
    }

    pub fn active_overlays<'a, 'b>(
        &'b self,
        removed_overlays: &'a mut Vec<Vec<u8>>,
    ) -> impl DoubleEndedIterator<Item = &'b OverlayFrame> + 'a
    where
        'b: 'a,
    {
        // Same filtering as `active_overlay_ids`, but iterated lazily: this runs for every scope
        // frame on every declaration or variable lookup, so it must not allocate.
        for name in &self.removed_overlays {
            if !removed_overlays.contains(name) {
                removed_overlays.push(name.clone());
            }
        }
        let removed_overlays: &'a Vec<Vec<u8>> = removed_overlays;

        self.active_overlays
            .iter()
            .filter(move |id| {
                !removed_overlays
                    .iter()
                    .any(|name| name == self.get_overlay_name(**id))
            })
            .map(|id| self.get_overlay(*id))
    }

    pub fn active_overlay_names(&self, removed_overlays: &mut Vec<Vec<u8>>) -> Vec<&[u8]> {
        self.active_overlay_ids(removed_overlays)
            .iter()
            .map(|id| self.get_overlay_name(*id))
            .collect()
    }

    pub fn get_overlay_name(&self, overlay_id: OverlayId) -> &[u8] {
        &self
            .overlays
            .get(overlay_id.get())
            .expect("internal error: missing overlay")
            .0
    }

    pub fn get_overlay(&self, overlay_id: OverlayId) -> &OverlayFrame {
        &self
            .overlays
            .get(overlay_id.get())
            .expect("internal error: missing overlay")
            .1
    }

    pub fn get_overlay_mut(&mut self, overlay_id: OverlayId) -> &mut OverlayFrame {
        &mut self
            .overlays
            .get_mut(overlay_id.get())
            .expect("internal error: missing overlay")
            .1
    }

    pub fn find_overlay(&self, name: &[u8]) -> Option<OverlayId> {
        self.overlays
            .iter()
            .position(|(n, _)| n == name)
            .map(OverlayId::new)
    }

    pub fn find_active_overlay(&self, name: &[u8]) -> Option<OverlayId> {
        self.overlays
            .iter()
            .position(|(n, _)| n == name)
            .map(OverlayId::new)
            .filter(|id| self.active_overlays.contains(id))
    }
}

#[derive(Debug, Clone)]
pub struct OverlayFrame {
    pub vars: HashMap<Vec<u8>, VarId>,
    pub predecls: DeclNameMap, // temporary storage for predeclarations
    pub decls: DeclNameMap,
    pub modules: HashMap<Vec<u8>, ModuleId>,
    pub type_aliases: HashMap<Vec<u8>, TypeAliasId>,
    pub shadowed_vars: Vec<VarId>,
    pub visibility: Visibility,
    pub origin: ModuleId, // The original module the overlay was created from
    pub prefixed: bool,   // Whether the overlay has definitions prefixed with its name
}

impl OverlayFrame {
    pub fn from_origin(origin: ModuleId, prefixed: bool) -> Self {
        Self {
            vars: HashMap::new(),
            predecls: DeclNameMap::new(),
            decls: DeclNameMap::new(),
            modules: HashMap::new(),
            type_aliases: HashMap::new(),
            shadowed_vars: Vec::new(),
            visibility: Visibility::new(),
            origin,
            prefixed,
        }
    }

    pub fn insert_decl(&mut self, name: Vec<u8>, decl_id: DeclId) -> Option<DeclId> {
        self.decls.insert(name, decl_id)
    }

    pub fn insert_module(&mut self, name: Vec<u8>, module_id: ModuleId) -> Option<ModuleId> {
        self.modules.insert(name, module_id)
    }

    pub fn insert_variable(&mut self, name: Vec<u8>, variable_id: VarId) -> Option<VarId> {
        let res = self.vars.insert(name, variable_id);
        if let Some(old_id) = res {
            self.shadowed_vars.push(old_id);
        }
        res
    }

    pub fn get_decl(&self, name: &[u8]) -> Option<DeclId> {
        self.decls.get(name).cloned()
    }
}

impl Default for Visibility {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for ScopeFrame {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod visibility_stack_tests {
    use super::*;

    /// A frame that hides `hidden` and explicitly shows `shown`.
    fn frame(hidden: &[usize], shown: &[usize]) -> Visibility {
        let mut visibility = Visibility::new();
        for id in hidden {
            visibility.hide_decl_id(&DeclId::new(*id));
        }
        for id in shown {
            visibility.use_decl_id(&DeclId::new(*id));
        }
        visibility
    }

    #[test]
    fn innermost_frame_with_an_entry_wins_past_the_inline_frames() {
        // Frame `i` hides decl `i` and shows decl `i + 1`; frames that hide nothing are skipped.
        let frames: Vec<Visibility> = (0..7).map(|i| frame(&[i], &[i + 1])).collect();
        let empty = Visibility::new();
        let mut stack = VisibilityStack::default();
        for visibility in &frames {
            stack.push(&empty);
            stack.push(visibility);
        }
        // Decl 0 is only hidden; every other decl is shown by the frame before the one hiding it.
        assert!(!stack.is_decl_id_visible(&DeclId::new(0)));
        for id in 1..8 {
            assert!(stack.is_decl_id_visible(&DeclId::new(id)), "decl {id}");
        }
        // A decl no frame mentions is visible.
        assert!(stack.is_decl_id_visible(&DeclId::new(100)));

        // The same frames with the hiding order reversed: the innermost entry decides.
        let mut stack = VisibilityStack::default();
        for visibility in frames.iter().rev() {
            stack.push(visibility);
        }
        assert!(!stack.is_decl_id_visible(&DeclId::new(6)));
        assert!(stack.is_decl_id_visible(&DeclId::new(7)));
        assert!(!stack.is_decl_id_visible(&DeclId::new(1)));
    }
}