//! What the rename verifier learns about implicit connections (`.name` and //! `.*`), and why it had to learn it. //! //! The rename contract was "the token stream may not change shape", and the //! resolution print compares references and declarations — and neither sees //! an implicit binding, because nothing is written for one: `.*` records no //! reference, and `.d` records only the port half. That is exactly how a //! rename used to cut `.*`/`.d` bindings and verify clean (M3 QA finding 1). //! So the fix is in two halves, and this is the second: //! //! 1. **The edits may insert, but only connection text.** A rename that //! rewrites implicit connections writes `(net)` after a shorthand or //! `.port(net)` into a list — never anything else. [`insertions_match`] //! holds such a file to the splice contract (the pieces, comments and //! attributes never dropped, no new parse error) *plus* a shape rule: every //! replacement is the old name becoming the new one, and every insertion //! is one of four connection templates spelling only those two names. //! 2. **Every implicit binding is printed, before and after.** For every //! instance in a watched file, [`bindings`] lists what each connection's //! port half and net half name — the net half of `.name` and of each port //! `.*` reaches looked up exactly where the language looks it up. The two //! lists must be equal, which is what refuses a dangling `.*`, a `.d` left //! pointing at nothing, a `.*` that starts binding the new name, and a //! connection added twice. //! //! Only bindings whose port or net spells the old or the new name are //! printed: a rename adds and removes declarations of those two names alone, //! so a lookup of any other name — and a port of any other name — cannot //! move. That is the watched-file argument, applied one level down. use std::path::{Path, PathBuf}; use vlog_parser::ast::{Connection, ExprKind, ListElem, SourceFile}; use vlog_parser::span::TextRange; use vlog_parser::token::TokenKind; use vlog_parser::{parse_source_tolerant, tokenize}; use vlog_semantic::{ resolve_external, ExternalResolutions, Semantics, Surface, SymbolKind, WorkspaceIndex, }; use super::ledger::{pieces_match, Split}; use super::{anchor_of, Anchor, Shifts}; use crate::rename::sites::{binds_implicitly, may_connect_implicitly, nearest, sites, Site}; use crate::rename::Scope; use crate::TextEdit; /// The rename's lexical contract for a file whose edits insert connection /// text: the old name replaced by the new one, the four connection templates /// inserted — each **where a connection list takes it** — and nothing else /// changed. pub(super) fn insertions_match( before: &str, after: &str, edits: &[TextEdit], names: [&str; 2], shown: &str, ) -> Result<(), String> { let refusal = |what: &str| format!("the rename would {what} in {shown}; no edits were made."); let Ok(old_tokens) = tokenize(before) else { return Err(refusal("edit a file lucisv cannot lex")); }; let slots = Slots::of(before); for edit in edits { let honest = match edit.range.is_empty() { true => template(&edit.new_text, names).is_some(), false => replaces_one_name(&old_tokens, edit, names), }; if !honest { return Err(refusal(&format!("write `{}`, which is not a rename", edit.new_text))); } if edit.range.is_empty() && !slots.takes(before, edit, edits, names) { let outside = format!("write `{}` outside a connection list", edit.new_text); return Err(refusal(&outside)); } } let split = Split::read(before, after, edits, shown, "the rename")?; pieces_match(&split, edits, shown, "the rename")?; let grew = parse_source_tolerant(after).1.len() > parse_source_tolerant(before).1.len(); match grew { true => Err(refusal("leave a connection list that does not parse")), false => Ok(()), } } /// A replacement is the one identifier token spelling the old name, replaced /// by the new name. fn replaces_one_name( tokens: &[vlog_parser::token::Token], edit: &TextEdit, names: [&str; 2], ) -> bool { let [old, new] = names; let covered: Vec<_> = tokens.iter().filter(|t| super::ledger::overlaps(t.span, edit.range)).collect(); matches!(covered.as_slice(), [only] if only.kind == TokenKind::Ident && only.span == edit.range && only.text == old && edit.new_text == new) } /// The four things a rename may insert. #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Template { /// `(X)`: a shorthand's net, written out. Tail, /// `,`: the comma before a `//` comment on a list's last element. Comma, /// `.X(Y)`: a connection after that comment, the comma already written. Connection, /// `, .X(Y)`: a connection appended to a list. Appended, } /// Which template an insertion spells — `X` and `Y` the old or the new name, /// no comment — or `None`. fn template(text: &str, names: [&str; 2]) -> Option