Better hint for non-zero em length (#4873)

Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
Sébastien d'Herbais de Thun 2024-09-02 15:33:43 +02:00 committed by GitHub
parent 1997db00f3
commit 1ccfaba88e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 10 deletions

View File

@ -5,7 +5,7 @@ use std::ops::{Add, Div, Mul, Neg};
use comemo::Tracked;
use ecow::{eco_format, EcoString};
use crate::diag::{At, Hint, HintedStrResult, SourceResult};
use crate::diag::{bail, HintedStrResult, SourceResult};
use crate::foundations::{func, scope, ty, Context, Fold, Repr, Resolve, StyleChain};
use crate::layout::{Abs, Em};
use crate::syntax::Span;
@ -81,12 +81,15 @@ impl Length {
if self.em == Em::zero() {
return Ok(());
}
Err(eco_format!(
bail!(
span,
"cannot convert a length with non-zero em units (`{}`) to {unit}",
self.repr()
))
.hint(eco_format!("use `length.abs.{unit}()` instead to ignore its em component"))
.at(span)
self.repr();
hint: "use `length.to-absolute()` to resolve its em component \
(requires context)";
hint: "or use `length.abs.{unit}()` instead to ignore its em component"
)
}
}

View File

@ -51,20 +51,24 @@
--- length-ignore-em-pt-hint ---
// Error: 2-21 cannot convert a length with non-zero em units (`-6pt + 10.5em`) to pt
// Hint: 2-21 use `length.abs.pt()` instead to ignore its em component
// Hint: 2-21 use `length.to-absolute()` to resolve its em component (requires context)
// Hint: 2-21 or use `length.abs.pt()` instead to ignore its em component
#(10.5em - 6pt).pt()
--- length-ignore-em-cm-hint ---
// Error: 2-12 cannot convert a length with non-zero em units (`3em`) to cm
// Hint: 2-12 use `length.abs.cm()` instead to ignore its em component
// Hint: 2-12 use `length.to-absolute()` to resolve its em component (requires context)
// Hint: 2-12 or use `length.abs.cm()` instead to ignore its em component
#(3em).cm()
--- length-ignore-em-mm-hint ---
// Error: 2-20 cannot convert a length with non-zero em units (`-226.77pt + 93em`) to mm
// Hint: 2-20 use `length.abs.mm()` instead to ignore its em component
// Hint: 2-20 use `length.to-absolute()` to resolve its em component (requires context)
// Hint: 2-20 or use `length.abs.mm()` instead to ignore its em component
#(93em - 80mm).mm()
--- length-ignore-em-inches-hint ---
// Error: 2-24 cannot convert a length with non-zero em units (`432pt + 4.5em`) to inches
// Hint: 2-24 use `length.abs.inches()` instead to ignore its em component
// Hint: 2-24 use `length.to-absolute()` to resolve its em component (requires context)
// Hint: 2-24 or use `length.abs.inches()` instead to ignore its em component
#(4.5em + 6in).inches()