Skip to content

Commit 83e55a5

Browse files
authored
Merge pull request #2319 from tweag/optional-span
Make spans in Labels optional, to avoid the cost of constructing them
2 parents 0809dbc + 28267e8 commit 83e55a5

File tree

7 files changed

+14
-24
lines changed

7 files changed

+14
-24
lines changed

cli/src/doctest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ fn doctest_transform(
447447
typ: eq_ty.clone(),
448448
label: Label {
449449
typ: Rc::new(eq_ty),
450-
span: expected_span,
450+
span: Some(expected_span),
451451
..Default::default()
452452
},
453453
}],

core/src/bytecode/ast/compat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ impl<'ast> FromAst<Type<'ast>> for term::LabeledType {
10851085
typ: typ.clone(),
10861086
label: label::Label {
10871087
typ: std::rc::Rc::new(typ),
1088-
span,
1088+
span: Some(span),
10891089
..Default::default()
10901090
},
10911091
}
@@ -1483,7 +1483,7 @@ impl<'ast> FromAst<Ast<'ast>> for term::RichTerm {
14831483
{
14841484
// unwrap(): we expect all position to be set in the new AST (should be using span
14851485
// directly in the future)
1486-
label.span = ast.pos.unwrap();
1486+
label.span = Some(ast.pos.unwrap());
14871487
}
14881488

14891489
result
@@ -1626,7 +1626,7 @@ fn merge_fields(
16261626
}
16271627
(t1, t2) => mk_term::op2(
16281628
BinaryOp::Merge(label::MergeLabel {
1629-
span: id_span,
1629+
span: Some(id_span),
16301630
kind: label::MergeKind::PiecewiseDef,
16311631
}),
16321632
RichTerm::new(t1, pos1),

core/src/error/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,9 @@ impl IntoDiagnostics for EvalError {
13561356
MergeKind::PiecewiseDef => "when combining the definitions of this field",
13571357
};
13581358

1359-
labels.push(secondary(&merge_label.span).with_message(span_label));
1359+
if let Some(merge_label_span) = &merge_label.span {
1360+
labels.push(secondary(merge_label_span).with_message(span_label));
1361+
}
13601362

13611363
fn push_merge_note(notes: &mut Vec<String>, typ: &str) {
13621364
notes.push(format!(

core/src/label.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::{collections::HashMap, rc::Rc};
66

77
use crate::{
88
eval::cache::{Cache as EvalCache, CacheIndex},
9-
files::Files,
109
identifier::LocIdent,
1110
position::{RawSpan, TermPos},
1211
term::{
@@ -281,7 +280,7 @@ pub struct Label {
281280
pub diagnostics: Vec<ContractDiagnostic>,
282281

283282
/// The position of the original contract.
284-
pub span: RawSpan,
283+
pub span: Option<RawSpan>,
285284

286285
/// The index corresponding to the value being checked. Set at run-time by the interpreter.
287286
pub arg_idx: Option<CacheIndex>,
@@ -411,11 +410,6 @@ impl Label {
411410
Label {
412411
typ: Rc::new(Type::from(TypeF::Number)),
413412
diagnostics: vec![ContractDiagnostic::new().with_message(String::from("testing"))],
414-
span: RawSpan {
415-
src_id: Files::new().add("<test>", String::from("empty")),
416-
start: 0.into(),
417-
end: 1.into(),
418-
},
419413
polarity: Polarity::Positive,
420414
..Default::default()
421415
}
@@ -508,11 +502,7 @@ impl Default for Label {
508502
fn default() -> Label {
509503
Label {
510504
typ: Rc::new(Type::from(TypeF::Dyn)),
511-
span: RawSpan {
512-
src_id: Files::new().add("<null>", String::from("")),
513-
start: 0.into(),
514-
end: 1.into(),
515-
},
505+
span: None,
516506
polarity: Polarity::Positive,
517507
diagnostics: Default::default(),
518508
arg_idx: Default::default(),
@@ -554,7 +544,7 @@ pub enum MergeKind {
554544
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
555545
pub struct MergeLabel {
556546
/// The span of the original merge (which might then decompose into many others).
557-
pub span: RawSpan,
547+
pub span: Option<RawSpan>,
558548
pub kind: MergeKind,
559549
}
560550

core/src/program.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ impl<EC: EvalCache> Program<EC> {
706706
term,
707707
Label {
708708
typ: std::rc::Rc::new(typ),
709-
span,
709+
span: Some(span),
710710
..Default::default()
711711
},
712712
))

core/src/term/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl LabeledType {
772772
typ: typ.clone(),
773773
label: Label {
774774
typ: Rc::new(typ),
775-
span,
775+
span: Some(span),
776776
..Default::default()
777777
},
778778
}

core/src/term/pattern/compile.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,11 @@ fn update_with_merge(record_id: LocIdent, id: LocIdent, field: Field) -> RichTer
178178

179179
let span = annot
180180
.iter()
181-
.map(|labeled_ty| labeled_ty.label.span)
181+
.filter_map(|labeled_ty| labeled_ty.label.span)
182182
.chain(pos_value)
183183
// We fuse all the definite spans together.
184184
// unwrap(): all span should come from the same file
185-
// unwrap(): we hope that at least one position is defined
186-
.reduce(|span1, span2| span1.fuse(span2).unwrap())
187-
.unwrap();
185+
.reduce(|span1, span2| span1.fuse(span2).unwrap());
188186

189187
let merge_label = MergeLabel {
190188
span,

0 commit comments

Comments
 (0)