diff --git a/quest/m3/README.md b/quest/m3/README.md index 552ba2609..cda0dfc57 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -20,7 +20,6 @@ generic std gap is planned here even when moq never calls it. - [BufMut's provided writes through a &mut](/quest/m3/ts-buf-mut-forward.md) - `put`, `put_bytes`, `put_uint`, and `put_int` of a `&mut Vec` or `&mut BytesMut` translate - [Trait objects](/quest/m3/ts-dyn.md) - `dyn Error`, `dyn Iterator`, `dyn Buf`, and `Arc` translate, so a consumer neither rewrites nor binds them - [Placeholders written to a crate's own fmt::Write](/quest/m3/ts-fmt-calls.md) - `write!` with placeholders into a crate's own writer translates, its `write_str` seeing each call std makes -- [Type-check the output before success](/quest/m3/ts-check-output.md) - translation fails at a Rust span on output `tsc` rejects, rather than at the consumer's `tsc` - [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - rust2-ts checks bindings through tsrs instead of `tsc` once it is a stable library - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's consumers, flattens, and collects drop as std's do - [Fresh writes in a body generic over a type](/quest/m3/ts-fresh-type-params.md) - a whole write into fresh memory holding a type parameter's values fills it, as one generic over a length alone does diff --git a/quest/m3/ts-check-output.md b/quest/m3/ts-check-output.md deleted file mode 100644 index e58da4f72..000000000 --- a/quest/m3/ts-check-output.md +++ /dev/null @@ -1,23 +0,0 @@ -# [M] Type-check the output before success - -## Goal - -[rust2-ts](/ts/README.md) fails translation, at the Rust span the source map names, on -output `tsc` rejects, so a typing gap fails loud where the consumer's crate translates -rather than in the consumer's own `tsc`, and measures what the check costs a moq-net -translation. - -## Plan - -Found by [the generic pointer fields](https://github.com/moq-dev/rust2/pull/371): a -generic type's `*mut T` field translated, and only `tsc` rejected it. `just ts test` -type-checks the fixtures and the nightly `just ts moq` moq's output, but nothing checks -another consumer's. Translation already runs the flake's `tsc` on bindings, so the -output could go through the same run. A rejection is a translator bug, so the error -should say so beside the span. - -Public API: translation fails where it succeeded on ill-typed output. - -## Related - -- [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - would make the check cheaper; this needs no wait diff --git a/quest/m3/ts-generic-byte-fields.md b/quest/m3/ts-generic-byte-fields.md index 100cd69ce..398a34a61 100644 --- a/quest/m3/ts-generic-byte-fields.md +++ b/quest/m3/ts-generic-byte-fields.md @@ -16,4 +16,7 @@ already read as its instance's type through a cast; a union of the element array the byte form, cast on read the same way, may fit, as may refusing the `u8` instance as `generic_erasure` refuses an `Option` of a type parameter that can be `undefined`. +The `reject` fixture's `mistyped` case tests the output's type check through this gap, +so the fix moves it to another typing gap translation has, if any is left. + Public API: the translator's output. diff --git a/quest/m3/ts-tsrs.md b/quest/m3/ts-tsrs.md index ace99aea8..e52959d62 100644 --- a/quest/m3/ts-tsrs.md +++ b/quest/m3/ts-tsrs.md @@ -4,8 +4,8 @@ Condition: [tsrs](https://blog.cloudflare.com/typescript-rust-port/), the Rust port of TypeScript, publishes a stable release usable as a Rust library. Then rust2-ts -type-checks bindings in process through it, with no `tsc` on `PATH`, failing at the -Rust item's span as today, and measures the translation's time against running `tsc`. +type-checks its output and bindings in process through it, with no `tsc` on `PATH`, +failing at the Rust span as today, and measures the translation's time against running `tsc`. Check: tsrs's releases, and whether it ships a crate (it is moving into Oxc). ## Plan diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index b442662e0..29ecf6866 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -2,6 +2,9 @@ ## Unreleased +- Every translation needs `tsc` 7.0 or later on `PATH`, not only one with bindings: it + type-checks the output, and fails at the Rust span of each error, where it succeeded + before. - A function the config's roots don't name takes each `&[u8]` or `&[u8; N]` parameter whose object its body never makes as its `data`, `start`, and `length`: `read(buf, form)` is `read(buf.data, buf.start, buf.length, form)`. A caller takes diff --git a/ts/README.md b/ts/README.md index a5fda3cc2..ca63ebd06 100644 --- a/ts/README.md +++ b/ts/README.md @@ -23,6 +23,12 @@ or received, such as a parameter, a field, or a call, rather than again at each holding one; any variable or temporary that holds a type whose JS value is ambiguous, such as `Option>`, is refused at its own span. +Once the output is written, `tsc` 7.0 or later, from `PATH`, type-checks it as strictly as +the runtime, but for unused names. Output it rejects is a bug in rust2-ts, and fails the +translation at the Rust span the source map names there, or else at the item's, and the +output is removed. The same run checks the bindings, and an error in translated code is +reported once they pass, since a wrong binding fails its callers too. + ```toml # ts.toml, beside the package's Cargo.toml unless --manifest-path says otherwise. package = "moq-net" @@ -552,7 +558,7 @@ binding never changes a `Copy` value in place. Nor does a binding change in plac takes by value or by `&`, or share with a clone what a `&mut` changes, and an integer it returns is in its Rust type's range. -Once the output is written, `tsc` 7.0 or later, from `PATH`, checks each export against the +The check of the output also checks each export against the translated signature of every instance translation calls, and each bound type's value. One returning `()` is checked against `=> undefined`, not `=> void`, which accepts any value, so its export is typed `: undefined`. A missing or mismatched binding fails the @@ -880,7 +886,8 @@ built natively for wasm32 and run beside its translation on the same inputs by from either end, and its `shared` feature adds a module of the dependency's name, with the errors in `refused.txt`, `rev.txt`, and `shared.txt`. - `reject` holds constructs outside the subset; its expected errors are `expected.txt`, - and its expected census is `census.md`. + and its expected census is `census.md`. `mistyped.toml` translates a typing gap that + `tsc` rejects, with the errors in `mistyped.txt`. `tests/spawn.ts` checks that `rt.spawn` yields to a timer after its 128-poll budget, and rejects when a future panics as it drops. `tests/retain.ts` runs a collection's `retain` diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index b91a2b7ec..e9355d31b 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` - Refusals: 218 -- Function instances: 270 +- Function instances: 273 - Generic functions no instance reached, lowered over their type parameters: 13 ## By construct diff --git a/ts/fixtures/reject/mistyped.toml b/ts/fixtures/reject/mistyped.toml new file mode 100644 index 000000000..51d11dc8e --- /dev/null +++ b/ts/fixtures/reject/mistyped.toml @@ -0,0 +1,4 @@ +# A typing gap translation doesn't refuse, which `tsc` fails at the Rust the source map +# names: `mistyped.txt` holds the errors. +package = "reject-fixture" +roots = ["mistyped"] diff --git a/ts/fixtures/reject/mistyped.txt b/ts/fixtures/reject/mistyped.txt new file mode 100644 index 000000000..63c27e962 --- /dev/null +++ b/ts/fixtures/reject/mistyped.txt @@ -0,0 +1,2 @@ +lib.Rows.ts:22:20 doesn't type-check, which is a bug in rust2-ts: error TS2339: Property 'data' does not exist on type 'u8[]'. at fixtures/reject/src/lib.rs:2161:3 +lib.ts:8:47 doesn't type-check, which is a bug in rust2-ts: error TS2740: Type 'ByteVec' is missing the following properties from type 'u8[]': pop, push, concat, join, and 28 more. at fixtures/reject/src/lib.rs:2167:4 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index 6c7abf2cd..94f0a7dd0 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -2148,3 +2148,21 @@ pub fn generic_park_rebound(mut w: Parking, next: Parking) -> i16 { w = next; unsafe { (*p).0.get() + w.park.0.get() } } + +/// A generic type's `Vec`, typed `T[]` though its `u8` instance holds an `rt.ByteVec`: +/// translation writes output `tsc` rejects, and fails at the Rust it maps to. +pub struct Rows { + pub items: Vec, +} + +impl Rows { + pub fn first(&self) -> T { + self.items[0] + } +} + +pub fn mistyped(bytes: Vec, words: Vec) -> u32 { + let a = Rows { items: bytes }; + let b = Rows { items: words }; + a.first() as u32 + b.first() as u32 +} diff --git a/ts/src/check.rs b/ts/src/check.rs new file mode 100644 index 000000000..defee1c0a --- /dev/null +++ b/ts/src/check.rs @@ -0,0 +1,271 @@ +//! The check of a written translation: `tsc` type-checks every translated module, and the +//! check module that holds each binding to what translated code takes it for. Translation +//! fails on any error, at the Rust span the source map names, and the output is removed. + +use std::collections::{HashMap, HashSet}; +use std::fmt::Write as _; +use std::path::{Path, PathBuf}; + +use rustc_middle::ty::TyCtxt; +use rustc_span::{BytePos, Span}; + +use crate::lower::check_module; +use crate::print::Printed; +use crate::program::{Program, item_sym, module_file}; +use crate::ts::Loc; + +/// The project `tsc` checks the output in, beside the check module: the runtime's own +/// settings, as `runtime/tsconfig.json` has them, but for the unused names a binding may +/// leave, which are its author's to lint, and the ambient types a caller's `node_modules` +/// may hold. A project of its own, since `tsc` refuses to check a file it names where a +/// caller's `tsconfig.json` is above it, naming only the files translation writes, which +/// a caller may keep others beside. +fn tsconfig(files: &[String]) -> String { + let files: Vec = files.iter().map(|file| json(file)).collect(); + format!( + r#"{{ + "compilerOptions": {{ + "strict": true, + "noEmit": true, + "pretty": false, + "target": "es2022", + "lib": ["es2022", "esnext.disposable", "dom"], + "module": "esnext", + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "erasableSyntaxOnly": true, + "skipLibCheck": true, + "types": [] + }}, + "files": [{}] +}} +"#, + files.join(", ") + ) +} + +/// Type-check the translation in `out`, whose modules `written` holds as printed, +/// reporting each error at the Rust it maps to, and then removing the translation. +pub fn typecheck(tcx: TyCtxt<'_>, program: &Program, out: &Path, written: &[(PathBuf, Printed)]) { + if written.is_empty() && program.checks.is_empty() { + return; + } + let failed = |msg: String| { + tcx.dcx().err(format!("rust2-ts: {msg}")); + _ = crate::program::clean(out); + }; + if let Err(err) = tsc_version() { + return failed(err); + } + // `tsc` names each file from its working directory, which the OS resolves through any + // symlink, so it's given the project by the real path too. + let real = match out.canonicalize() { + Ok(real) => real, + Err(err) => return failed(format!("resolving {}: {err}", out.display())), + }; + let module = check_module(); + let check_file = module_file(&module); + let path = real.join(&check_file); + let mut files: Vec = written + .iter() + .map(|(file, _)| format!("../{}", file.to_string_lossy())) + .collect(); + let checks = match program.checks.is_empty() { + true => None, + false => { + let items: Vec<_> = program.checks.iter().map(|check| &check.item).collect(); + let printed = match crate::print::module(program, &module, &items, &path) { + Ok(printed) => printed, + Err(err) => return failed(err), + }; + if let Err(err) = crate::program::write_file(&path, &printed.code) { + return failed(err); + } + files.push(path.file_name().expect("a file name").to_string_lossy().into_owned()); + Some(printed) + } + }; + let project = path.with_extension("json"); + if let Err(err) = crate::program::write_file(&project, &tsconfig(&files)) { + _ = std::fs::remove_file(&path); + return failed(err); + } + let output = std::process::Command::new("tsc") + .arg("-p") + .arg(&project) + .current_dir(&real) + .output(); + _ = std::fs::remove_file(&path); + _ = std::fs::remove_file(&project); + let output = match output { + Ok(output) => output, + Err(err) => return failed(format!("running tsc, which checks the translation: {err}")), + }; + if output.status.success() { + return; + } + let mut modules: HashMap<&Path, &Printed> = written + .iter() + .map(|(file, printed)| (file.as_path(), printed)) + .collect(); + if let Some(checks) = &checks { + modules.insert(&check_file, checks); + } + let by_sym: HashMap<_, _> = program + .checks + .iter() + .map(|check| (item_sym(&check.item), check)) + .collect(); + let text = String::from_utf8_lossy(&output.stdout); + // Where the output is, which a message naming a module spells out. + let real_dir = format!("{}/", real.display()); + // Each error, at its Rust span if it has one: those of the bindings and those of the + // translated modules apart. Each item's instances of a generic function fail alike, so + // each failure is reported once. + let mut bound: Vec<(Option, String)> = Vec::new(); + let mut translated: Vec<(Option, String)> = Vec::new(); + let mut seen = HashSet::new(); + for diagnostic in diagnostics(&text) { + let diagnostic = diagnostic.replace(&real_dir, ""); + // `file(line,col): error TS2322: ...`, with a one-based line and column. + let located = diagnostic.split_once('(').and_then(|(file, rest)| { + let (at, msg) = rest.split_once("): ")?; + let (line, col) = at.split_once(',')?; + let printed = modules.get(Path::new(file))?; + Some((file, line.parse::().ok()?, col.parse::().ok()?, msg, printed)) + }); + let Some((file, line, col, msg, printed)) = located else { + if seen.insert((None, diagnostic.clone())) { + bound.push((None, format!("tsc: {diagnostic}"))); + } + continue; + }; + let (line, col) = (line - 1, col - 1); + let item = printed.items.iter().rev().find(|&&(start, _)| start <= line).copied(); + if Path::new(file) == check_file { + let check = item.and_then(|(_, sym)| by_sym.get(&sym)); + if seen.insert((check.map(|check| check.span), msg.to_string())) { + bound.push(match check { + Some(check) => (Some(check.span), format!("{} doesn't type-check: {msg}", check.what)), + None => (None, format!("tsc: {diagnostic}")), + }); + } + continue; + } + // The Rust the source map names here, if it maps this item, or else the item's. + let at = printed.tokens.partition_point(|&(l, c, _)| (l, c) <= (line, col)); + let span = match (at.checked_sub(1).map(|at| printed.tokens[at]), item) { + (Some((l, _, loc)), Some((start, _))) if l >= start => Some(span(program, loc)), + (_, Some((_, sym))) => Some(program.syms[sym].span).filter(|span| !span.is_dummy()), + _ => None, + }; + if seen.insert((span, msg.to_string())) { + translated.push(( + span, + format!( + "{file}:{}:{} doesn't type-check, which is a bug in rust2-ts: {msg}", + line + 1, + col + 1 + ), + )); + } + } + // A binding that fails, or whose file does, fails the translated code calling it too, + // which is no bug of translation's: that is reported once the bindings check. + let errors = if bound.is_empty() { translated } else { bound }; + if errors.is_empty() { + tcx.dcx().err(format!( + "rust2-ts: tsc failed: {text}{}", + String::from_utf8_lossy(&output.stderr) + )); + } + for (span, msg) in errors { + match span { + Some(span) => _ = tcx.dcx().span_err(span, format!("rust2-ts: {msg}")), + None => _ = tcx.dcx().err(format!("rust2-ts: {msg}")), + } + } + _ = crate::program::clean(out); +} + +/// The span where `loc` is. +fn span(program: &Program, loc: Loc) -> Span { + let source = &program.sources[loc.file as usize]; + let line: usize = source + .text + .split_inclusive('\n') + .take(loc.line as usize) + .map(str::len) + .sum(); + let mut units = 0; + let col: usize = source.text[line..] + .chars() + .take_while(|c| { + units += c.len_utf16() as u32; + units <= loc.col + }) + .map(char::len_utf8) + .sum(); + let lo = source.start + BytePos((line + col) as u32); + Span::with_root_ctxt(lo, lo) +} + +/// `text` as a JSON string. +fn json(text: &str) -> String { + let mut out = String::from('"'); + for c in text.chars() { + match c { + '"' | '\\' => { + out.push('\\'); + out.push(c); + } + c if c.is_control() => write!(out, "\\u{:04x}", c as u32).unwrap(), + c => out.push(c), + } + } + out.push('"'); + out +} + +/// The oldest `tsc` the check project is written for. +const TSC_MAJOR: u32 = 7; + +/// Refuse a `tsc` on `PATH` older than [`TSC_MAJOR`], which would fail the check project's +/// options rather than the translation. +fn tsc_version() -> Result<(), String> { + let output = std::process::Command::new("tsc") + .arg("--version") + .output() + .map_err(|err| format!("running tsc, which checks the translation: {err}"))?; + let text = String::from_utf8_lossy(&output.stdout); + // `Version 7.0.2`. + let major = text + .split_whitespace() + .last() + .and_then(|version| version.split('.').next()) + .and_then(|major| major.parse::().ok()); + match major { + Some(major) if major >= TSC_MAJOR => Ok(()), + _ => Err(format!( + "tsc {}, which checks the translation, must be {TSC_MAJOR}.0 or later", + text.trim() + )), + } +} + +/// Each of `tsc`'s diagnostics, with the lines that go on with it. +fn diagnostics(text: &str) -> Vec { + let mut all: Vec = Vec::new(); + for line in text.lines() { + match all.last_mut() { + Some(last) if line.starts_with(char::is_whitespace) => { + last.push('\n'); + last.push_str(line); + } + _ if line.trim().is_empty() => {} + _ => all.push(line.to_string()), + } + } + all +} diff --git a/ts/src/driver.rs b/ts/src/driver.rs index 5d34563e8..f39c8f7a3 100644 --- a/ts/src/driver.rs +++ b/ts/src/driver.rs @@ -9,7 +9,7 @@ use rustc_interface::interface::Compiler; use rustc_middle::ty::TyCtxt; use crate::config::{self, Config}; -use crate::{cli, lower, program}; +use crate::{check, cli, lower, program}; /// Run as rustc, with `args` as cargo passed them: the wrapped rustc's path first. pub fn run(args: Vec) -> ExitCode { @@ -84,9 +84,9 @@ impl Callbacks for Translate { // Only a program rustc accepts is translated. let written = match &self.output { Output::Dir(out) => match lower::translate(tcx, &self.config) { - Some(program) if tcx.dcx().has_errors().is_none() => { - program.write(out).map(|()| lower::typecheck(tcx, &program, out)) - } + Some(program) if tcx.dcx().has_errors().is_none() => program + .write(out) + .map(|written| check::typecheck(tcx, &program, out, &written)), _ => Ok(()), }, Output::Census(path) => match lower::census(tcx, &self.config) { diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs index ebece3d4e..74fc6e02d 100644 --- a/ts/src/lower/bind.rs +++ b/ts/src/lower/bind.rs @@ -3,7 +3,7 @@ //! defines, trait impls included, calls the binding file's export named for the Rust //! function: one export serves every instance, so a generic one is a generic TS function. //! `tsc` checks each export against every instance's translated signature once the output -//! is written. +//! is written, as [`crate::check`] does. use std::path::{Component, Path, PathBuf}; @@ -14,7 +14,7 @@ use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt, TypeVisitableExt}; use rustc_span::Span; use super::{Translator, camel, names}; -use crate::program::{Bound, Check, ModPath, Program, SymKind, Symbol, module_file}; +use crate::program::{Bound, Check, ModPath, SymKind, Symbol, module_file}; use crate::ts::{Expr, Function, Item, Param, StmtKind, SymId, Type}; /// An item `ts.toml` binds. @@ -441,167 +441,6 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } } -/// The project `tsc` checks the check module in, beside it: the runtime's own settings, as -/// `runtime/tsconfig.json` has them, but for the unused names a binding may leave, which -/// are its author's to lint, and the ambient types a caller's `node_modules` may hold. A -/// project of its own, since `tsc` refuses to check a file it names where a caller's -/// `tsconfig.json` is above it. -const TSCONFIG: &str = r#"{ - "compilerOptions": { - "strict": true, - "noEmit": true, - "pretty": false, - "target": "es2022", - "lib": ["es2022", "esnext.disposable", "dom"], - "module": "esnext", - "moduleResolution": "bundler", - "allowImportingTsExtensions": true, - "verbatimModuleSyntax": true, - "erasableSyntaxOnly": true, - "skipLibCheck": true, - "types": [] - }, - "files": ["bind.ts"] -} -"#; - -/// Type-check the bindings the translation in `out` reaches against what it takes each -/// for, reporting each that fails at its Rust item, and then removing the translation. -pub fn typecheck(tcx: TyCtxt<'_>, program: &Program, out: &Path) { - if program.checks.is_empty() { - return; - } - let failed = |msg: String| { - tcx.dcx().err(format!("rust2-ts: {msg}")); - _ = crate::program::clean(out); - }; - if let Err(err) = tsc_version() { - return failed(err); - } - // `tsc` names each file from its working directory, which the OS resolves through any - // symlink, so it's given the project by the real path too. - let real = match out.canonicalize() { - Ok(real) => real, - Err(err) => return failed(format!("resolving {}: {err}", out.display())), - }; - let module = check_module(); - let path = real.join(module_file(&module)); - let items: Vec<&Item> = program.checks.iter().map(|check| &check.item).collect(); - let printed = match crate::print::module(program, &module, &items, &path) { - Ok(printed) => printed, - Err(err) => return failed(err), - }; - let project = path.with_extension("json"); - if let Err(err) = - crate::program::write_file(&path, &printed.code).and_then(|()| crate::program::write_file(&project, TSCONFIG)) - { - return failed(err); - } - let name = path.file_name().expect("a file name").to_string_lossy().into_owned(); - let output = std::process::Command::new("tsc") - .arg("-p") - .arg(&project) - .current_dir(path.parent().expect("the runtime's directory")) - .output(); - _ = std::fs::remove_file(&path); - _ = std::fs::remove_file(&project); - let output = match output { - Ok(output) => output, - Err(err) => return failed(format!("running tsc, which checks the bindings: {err}")), - }; - if output.status.success() { - return; - } - // The check each line of the module is in, by the function it starts at. - let mut starts = Vec::new(); - for (line, text) in printed.code.lines().enumerate() { - if let Some(rest) = text.split("function check$").nth(1) - && let Some(index) = rest.split('(').next().and_then(|n| n.parse::().ok()) - { - starts.push((line + 1, index)); - } - } - let text = String::from_utf8_lossy(&output.stdout); - let prefix = format!("{name}("); - // Where the output is, which a message naming a module spells out. - let dir = format!("{}/", real.display()); - // Each item's instances of a generic function fail alike, so each failure is reported once. - let mut reported = std::collections::HashSet::new(); - for diagnostic in diagnostics(&text) { - // One in a binding's file names it from the runtime's directory. - let diagnostic = diagnostic.strip_prefix("../").unwrap_or(&diagnostic); - let diagnostic = diagnostic.replace(&dir, ""); - let check = diagnostic - .strip_prefix(&prefix) - .and_then(|rest| rest.split([',', ')']).next()) - .and_then(|line| line.parse::().ok()) - .and_then(|line| starts.iter().rev().find(|&&(start, _)| start <= line)) - .map(|&(_, index)| &program.checks[index]); - let msg = diagnostic.split_once(": ").map_or(diagnostic.as_str(), |(_, msg)| msg); - if !reported.insert((check.map(|check| check.span), msg.to_string())) { - continue; - } - match check { - Some(check) => { - _ = tcx.dcx().span_err( - check.span, - format!("rust2-ts: {} doesn't type-check: {msg}", check.what), - ) - } - None => _ = tcx.dcx().err(format!("rust2-ts: tsc: {diagnostic}")), - } - } - if reported.is_empty() { - tcx.dcx().err(format!( - "rust2-ts: tsc failed: {text}{}", - String::from_utf8_lossy(&output.stderr) - )); - } - _ = crate::program::clean(out); -} - -/// The oldest `tsc` the check project is written for. -const TSC_MAJOR: u32 = 7; - -/// Refuse a `tsc` on `PATH` older than [`TSC_MAJOR`], which would fail the check project's -/// options rather than the bindings. -fn tsc_version() -> Result<(), String> { - let output = std::process::Command::new("tsc") - .arg("--version") - .output() - .map_err(|err| format!("running tsc, which checks the bindings: {err}"))?; - let text = String::from_utf8_lossy(&output.stdout); - // `Version 7.0.2`. - let major = text - .split_whitespace() - .last() - .and_then(|version| version.split('.').next()) - .and_then(|major| major.parse::().ok()); - match major { - Some(major) if major >= TSC_MAJOR => Ok(()), - _ => Err(format!( - "tsc {}, which checks the bindings, must be {TSC_MAJOR}.0 or later", - text.trim() - )), - } -} - -/// Each of `tsc`'s diagnostics, with the lines that go on with it. -fn diagnostics(text: &str) -> Vec { - let mut all: Vec = Vec::new(); - for line in text.lines() { - match all.last_mut() { - Some(last) if line.starts_with(char::is_whitespace) => { - last.push('\n'); - last.push_str(line); - } - _ if line.trim().is_empty() => {} - _ => all.push(line.to_string()), - } - } - all -} - /// The module `tsc` checks the bindings in, which no Rust module or binding can be: the /// runtime's directory holds only the runtime. pub fn check_module() -> ModPath { diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index 69d59194a..3aaf04cfc 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -6,7 +6,7 @@ //! a type is a module-level one taking `self` first, so a bundler drops what goes unused. mod bind; -pub use bind::typecheck; +pub use bind::check_module; mod body; mod collections; mod consts; @@ -870,6 +870,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { self.program.sources.push(Source { path, text: text.to_string(), + start: file.start_pos, }); Some(self.program.sources.len() as u32 - 1) } diff --git a/ts/src/main.rs b/ts/src/main.rs index 732bec36d..e29eed78b 100644 --- a/ts/src/main.rs +++ b/ts/src/main.rs @@ -26,6 +26,7 @@ extern crate rustc_target; extern crate rustc_trait_selection; mod census; +mod check; mod cli; mod config; mod driver; diff --git a/ts/src/print.rs b/ts/src/print.rs index 2662e3ab2..b6e47f6f6 100644 --- a/ts/src/print.rs +++ b/ts/src/print.rs @@ -6,13 +6,17 @@ use std::path::{Component, Path, PathBuf}; use oxc_sourcemap::SourceMapBuilder; -use crate::program::{HEADER, ModPath, Program, SymKind, module_file, unique}; +use crate::program::{HEADER, ModPath, Program, SymKind, item_sym, module_file, unique}; use crate::ts::{Ctor, Expr, FieldInit, Function, Item, Key, Loc, Param, Stmt, StmtKind, SymId, Type}; pub struct Printed { pub code: String, /// The source map, as JSON. pub map: String, + /// Its mappings: each zero-based line and UTF-16 column of `code` and the Rust it maps to. + pub tokens: Vec<(u32, u32, Loc)>, + /// Each item's symbol by the zero-based line of `code` it starts on. + pub items: Vec<(u32, SymId)>, } /// Print one module's items to the file at `path`. @@ -30,10 +34,12 @@ pub fn module(program: &Program, module: &ModPath, items: &[&Item], path: &Path) renames: HashMap::new(), aliases: Vec::new(), }; + let mut starts = Vec::new(); for (i, item) in items.iter().enumerate() { if i > 0 { p.newline(); } + starts.push((p.line, item_sym(item))); p.item(item); } if !p.aliases.is_empty() { @@ -112,16 +118,23 @@ pub fn module(program: &Program, module: &ModPath, items: &[&Item], path: &Path) let mut map = SourceMapBuilder::default(); map.set_file(&file_name); let mut ids = HashMap::new(); - for (line, col, loc) in &p.tokens { + let tokens: Vec<(u32, u32, Loc)> = p + .tokens + .into_iter() + .map(|(line, col, loc)| (line + offset, col, loc)) + .collect(); + for (line, col, loc) in &tokens { let id = *ids.entry(loc.file).or_insert_with(|| { let source = &program.sources[loc.file as usize]; map.add_source_and_content(&source_names[loc.file as usize], &source.text) }); - map.add_token(line + offset, *col, loc.line, loc.col, Some(id), None); + map.add_token(*line, *col, loc.line, loc.col, Some(id), None); } Ok(Printed { code, map: map.into_sourcemap().to_json_string(), + tokens, + items: starts.into_iter().map(|(line, sym)| (line + offset, sym)).collect(), }) } diff --git a/ts/src/program.rs b/ts/src/program.rs index 04755028c..2bd0356de 100644 --- a/ts/src/program.rs +++ b/ts/src/program.rs @@ -95,6 +95,8 @@ pub struct FieldInfo { pub struct Source { pub path: PathBuf, pub text: String, + /// Where rustc's source map starts it, which spans count from. + pub start: rustc_span::BytePos, } #[derive(Debug, Default)] @@ -480,8 +482,9 @@ impl Program { Ok(()) } - /// Write every module, the runtime, and their source maps to `out`. - pub fn write(&self, out: &Path) -> Result<(), String> { + /// Write every module, the runtime, and their source maps to `out`, returning each + /// module as printed, by its file. + pub fn write(&self, out: &Path) -> Result, String> { // A copy, or a clean, would replace or remove the source. if let Ok(dir) = out.canonicalize() && let Some(bound) = self @@ -530,16 +533,19 @@ impl Program { )); } } + let mut written = Vec::new(); for (module, items) in modules { - let path = out.join(module_file(module)); + let file = module_file(module); + let path = out.join(&file); let printed = print::module(self, module, &items, &path)?; write_file(&path, &printed.code)?; write_file(&path.with_extension("ts.map"), &printed.map)?; + written.push((file, printed)); } for (module, bound) in &self.bound { write_file(&out.join(module_file(module)), &format!("{HEADER}\n{}", bound.text))?; } - Ok(()) + Ok(written) } /// How a module's importers bind it whole, if they do: a type's module, or a binding's diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index 41e37c162..a181b5d11 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -439,6 +439,10 @@ export const run = (input: rt.ByteSlice) => script(rt.byteSliceView(input)); fn reject() { let (out, output) = translate("reject"); rejected(&out, &output, &fixture("reject").join("expected.txt")); + let out = scratch().join("reject").join("mistyped"); + let config = fixture("reject").join("mistyped.toml"); + let output = driver("reject", &config, &["--out".as_ref(), out.as_ref()], "build"); + rejected(&out, &output, &fixture("reject").join("mistyped.txt")); } /// Bindings stand in for what translation refuses, `bytes::Bytes` and the crate's own