Skip to main content

Module substitution

Module substitution 

Source
Expand description

What normalization erased: which name became which.

Type-2 detection works by not looking at identifiers, so two occurrences that differ only in their names come out equal. That is the right thing for finding the duplication and the wrong thing for judging it, because the commonest false positive in a typed language is a set of routines the language forced apart — one per integer width, one per float width — and the only thing that says so is the very substitution normalization threw away.

This keeps it. A Witness is the list of name changes that turn one occurrence into the other, with the two questions worth asking of them already answered: whether every change is the same width being swapped for another, and whether any of them changed a literal rather than a name.

§How the two occurrences are lined up

By the very thing normalization kept. Two tokens may stand in the same place when they are the same kind — TokenKind::tag decides that, and it is the detector’s own answer to what counts as the same token once the spelling is gone. So the alignment is computed over the normalized run and the substitutions are read off the raw one: exactly the two halves of what a Type-2 match is.

Where an occurrence has tokens the other does not, those are edits rather than substitutions, and Witness::edits counts them. Nothing here decides what an edit means; a rule reading a witness does.

§What it does not do

It gives up on a pair too large to align, because the alignment is quadratic and a bound that is never hit is a bound nobody has tested. Being unable to say is recorded as None, never as an empty witness.

Structs§

Change
One name replaced by another.
Witness
The substitutions that turn one occurrence into another.

Constants§

SUBSTITUTION_VERSION
Version of the witness rules, for recording alongside the other detector versions when something acts on one.

Functions§

witness
The substitutions turning left into right, or None when the pair is too large to align.