Skip to content

Commit

Permalink
Fix more compiler errors (remaining: tc::eq and tc::error)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannham committed Dec 18, 2024
1 parent 3b9b3db commit 6b7ecda
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 72 deletions.
11 changes: 3 additions & 8 deletions core/src/bytecode/ast/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,16 @@ impl<'ast> TraverseAlloc<'ast, Ast<'ast>> for FieldDef<'ast> {

fn traverse_ref<S, U>(
&self,
alloc: &'ast AstAlloc,
f: &mut dyn FnMut(&Ast<'ast>, &S) -> TraverseControl<S, U>,
scope: &S,
) -> Option<U> {
self.path
.iter()
.find_map(|elem| match elem {
FieldPathElem::Ident(_) => None,
FieldPathElem::Expr(expr) => expr.traverse_ref(alloc, f, scope),
})
.or_else(|| self.metadata.annotation.traverse_ref(alloc, f, scope))
.or_else(|| {
self.value
.as_ref()
.and_then(|v| v.traverse_ref(alloc, f, scope))
FieldPathElem::Expr(expr) => expr.traverse_ref(f, scope),
})
.or_else(|| self.metadata.annotation.traverse_ref(f, scope))
.or_else(|| self.value.as_ref().and_then(|v| v.traverse_ref(f, scope)))
}
}
20 changes: 8 additions & 12 deletions core/src/bytecode/ast/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ impl<'ast> TraverseAlloc<'ast, Type<'ast>> for Type<'ast> {

fn traverse_ref<S, U>(
&self,
alloc: &'ast AstAlloc,
f: &mut dyn FnMut(&Type<'ast>, &S) -> TraverseControl<S, U>,
state: &S,
) -> Option<U> {
Expand All @@ -116,14 +115,14 @@ impl<'ast> TraverseAlloc<'ast, Type<'ast>> for Type<'ast> {
| TypeF::Var(_)
| TypeF::Enum(_)
| TypeF::Wildcard(_) => None,
TypeF::Contract(ast) => ast.traverse_ref(alloc, f, state),
TypeF::Contract(ast) => ast.traverse_ref(f, state),
TypeF::Arrow(t1, t2) => t1
.traverse_ref(alloc, f, state)
.or_else(|| t2.traverse_ref(alloc, f, state)),
.traverse_ref(f, state)
.or_else(|| t2.traverse_ref(f, state)),
TypeF::Forall { body: t, .. }
| TypeF::Dict { type_fields: t, .. }
| TypeF::Array(t) => t.traverse_ref(alloc, f, state),
TypeF::Record(rrows) => rrows.traverse_ref(alloc, f, state),
| TypeF::Array(t) => t.traverse_ref(f, state),
TypeF::Record(rrows) => rrows.traverse_ref(f, state),
}
}
}
Expand Down Expand Up @@ -153,15 +152,13 @@ impl<'ast> TraverseAlloc<'ast, Ast<'ast>> for Type<'ast> {

fn traverse_ref<S, U>(
&self,
alloc: &'ast AstAlloc,
f: &mut dyn FnMut(&Ast<'ast>, &S) -> TraverseControl<S, U>,
state: &S,
) -> Option<U> {
self.traverse_ref(
alloc,
&mut |ty: &Type, s: &S| match &ty.typ {
TypeF::Contract(t) => {
if let Some(ret) = t.traverse_ref(alloc, f, s) {
if let Some(ret) = t.traverse_ref(f, s) {
TraverseControl::Return(ret)
} else {
TraverseControl::SkipBranch
Expand Down Expand Up @@ -198,15 +195,14 @@ impl<'ast> TraverseAlloc<'ast, Type<'ast>> for RecordRows<'ast> {

fn traverse_ref<S, U>(
&self,
alloc: &'ast AstAlloc,
f: &mut dyn FnMut(&Type<'ast>, &S) -> TraverseControl<S, U>,
state: &S,
) -> Option<U> {
match &self.0 {
RecordRowsF::Extend { row, tail } => row
.typ
.traverse_ref(alloc, f, state)
.or_else(|| tail.traverse_ref(alloc, f, state)),
.traverse_ref(f, state)
.or_else(|| tail.traverse_ref(f, state)),
_ => None,
}
}
Expand Down
5 changes: 2 additions & 3 deletions core/src/bytecode/typecheck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ pub mod subtyping;
pub mod unif;

use error::*;
use indexmap::IndexMap;
use operation::PrimOpType;
use pattern::{PatternTypeData, PatternTypes};
use record::Resolve;
Expand Down Expand Up @@ -1989,7 +1988,7 @@ fn check<'ast, V: TypecheckVisitor<'ast>>(

let start_ctxt = ctxt.clone();

let mut typed_bindings: Result<_, _> = bindings
let typed_bindings: Result<Vec<_>, _> = bindings
.iter()
.map(|binding| -> Result<_, TypecheckError> {
// See [^separate-alias-treatment].
Expand Down Expand Up @@ -2031,7 +2030,7 @@ fn check<'ast, V: TypecheckVisitor<'ast>>(
);
}

Ok((binding.value, ty_let))
Ok((&binding.value, ty_let))
})
.collect();

Expand Down
8 changes: 4 additions & 4 deletions core/src/bytecode/typecheck/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,16 +461,16 @@ impl<'ast> PatternTypes<'ast> for FieldPattern<'ast> {
// | Number}` instead, at least outside of a statically typed code block. But before
// this happens, we special case the old behavior and eschew unification.
(Some(annot_ty), PatternData::Any(id), TypecheckMode::Walk) => {
let ty_row = UnifType::from_type(annot_ty.typ.clone(), &ctxt.term_env);
let ty_row = UnifType::from_type(annot_ty.clone(), &ctxt.term_env);
pt_state.bindings.push((*id, ty_row.clone()));
ty_row
}
(Some(annot_ty), PatternData::Wildcard, TypecheckMode::Walk) => {
UnifType::from_type(annot_ty.typ.clone(), &ctxt.term_env)
UnifType::from_type(annot_ty.clone(), &ctxt.term_env)
}
(Some(annot_ty), _, _) => {
let pos = annot_ty.typ.pos;
let annot_uty = UnifType::from_type(annot_ty.typ.clone(), &ctxt.term_env);
let pos = annot_ty.pos;
let annot_uty = UnifType::from_type(annot_ty.clone(), &ctxt.term_env);

let ty_row = self
.pattern
Expand Down
9 changes: 7 additions & 2 deletions core/src/bytecode/typecheck/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ impl<'ast> ResolvedRecord<'ast> {
let mut need_unif_step = HashSet::new();

for (id, field) in &self.stat_fields {
let uty_apprt = field.apparent_type(Some(&ctxt.type_env), Some(state.resolver));
let uty_apprt = field.apparent_type(
&state.ast_alloc,
Some(&ctxt.type_env),
Some(state.resolver),
);

// `Approximated` corresponds to the case where the type isn't obvious (annotation
// or constant), and thus to case 2. above
Expand Down Expand Up @@ -512,6 +516,7 @@ impl<'ast> HasApparentType<'ast> for ResolvedField<'ast> {
// there is no value, `Approximated(Dyn)` is returned.
fn apparent_type(
&self,
ast_alloc: &'ast AstAlloc,
env: Option<&TypeEnv<'ast>>,
resolver: Option<&dyn ImportResolver>,
) -> ApparentType<'ast> {
Expand All @@ -522,7 +527,7 @@ impl<'ast> HasApparentType<'ast> for ResolvedField<'ast> {
// merge expression is also `Dyn`.
_ if !self.resolved.is_empty() => ApparentType::Approximated(Type::from(TypeF::Dyn)),
[] => ApparentType::Approximated(Type::from(TypeF::Dyn)),
[def] => def.apparent_type(env, resolver),
[def] => def.apparent_type(ast_alloc, env, resolver),
_ => self
.first_annot()
.map(|ty| ApparentType::Annotated(ty))
Expand Down
33 changes: 18 additions & 15 deletions core/src/bytecode/typecheck/unif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ impl<'ast> UnifTable<'ast> {
/// the map to be rather sparse, we use a `HashMap` instead of a `Vec`.
pub type RowConstrs = HashMap<VarId, HashSet<Ident>>;

pub(super) trait PropagateConstrs {
pub(super) trait PropagateConstrs<'ast> {
/// Check that unifying a variable with a type doesn't violate rows constraints, and update the
/// row constraints of the unified type accordingly if needed.
///
Expand All @@ -1063,22 +1063,25 @@ pub(super) trait PropagateConstrs {
/// don't constrain `u`, `u` could be unified later with a row type `{a : String}` which violates
/// the original constraint on `p`. Thus, when unifying `p` with `u` or a row ending with `u`,
/// `u` must inherit all the constraints of `p`.
fn propagate_constrs(&self, constr: &mut RowConstrs, var_id: VarId)
-> Result<(), RowUnifError>;
fn propagate_constrs(
&self,
constr: &mut RowConstrs,
var_id: VarId,
) -> Result<(), RowUnifError<'ast>>;
}

impl<'ast> PropagateConstrs for UnifRecordRows<'ast> {
impl<'ast> PropagateConstrs<'ast> for UnifRecordRows<'ast> {
fn propagate_constrs(
&self,
constr: &mut RowConstrs,
var_id: VarId,
) -> Result<(), RowUnifError> {
) -> Result<(), RowUnifError<'ast>> {
fn propagate<'ast>(
constr: &mut RowConstrs,
var_id: VarId,
var_constr: HashSet<Ident>,
rrows: &UnifRecordRows<'ast>,
) -> Result<(), RowUnifError> {
) -> Result<(), RowUnifError<'ast>> {
match rrows {
UnifRecordRows::Concrete {
rrows: RecordRowsF::Extend { row, .. },
Expand Down Expand Up @@ -1111,18 +1114,18 @@ impl<'ast> PropagateConstrs for UnifRecordRows<'ast> {
}
}

impl<'ast> PropagateConstrs for UnifEnumRows<'ast> {
impl<'ast> PropagateConstrs<'ast> for UnifEnumRows<'ast> {
fn propagate_constrs(
&self,
constr: &mut RowConstrs,
var_id: VarId,
) -> Result<(), RowUnifError> {
) -> Result<(), RowUnifError<'ast>> {
fn propagate<'ast>(
constr: &mut RowConstrs,
var_id: VarId,
var_constr: HashSet<Ident>,
erows: &UnifEnumRows<'ast>,
) -> Result<(), RowUnifError> {
) -> Result<(), RowUnifError<'ast>> {
match erows {
UnifEnumRows::Concrete {
// If the row is an enum tag (ie `typ` is `None`), it can't cause any conflict.
Expand Down Expand Up @@ -1180,14 +1183,14 @@ pub(super) trait Unify<'ast> {
}

impl<'ast> Unify<'ast> for UnifType<'ast> {
type Error = UnifError;
type Error = UnifError<'ast>;

fn unify(
self,
t2: UnifType<'ast>,
state: &mut State<'ast, '_>,
ctxt: &Context<'ast>,
) -> Result<(), UnifError> {
) -> Result<(), UnifError<'ast>> {
let t1 = self.into_root(state.table);
let t2 = t2.into_root(state.table);

Expand Down Expand Up @@ -1355,14 +1358,14 @@ impl<'ast> Unify<'ast> for UnifType<'ast> {
}

impl<'ast> Unify<'ast> for UnifEnumRows<'ast> {
type Error = RowUnifError;
type Error = RowUnifError<'ast>;

fn unify(
self,
uerows2: UnifEnumRows<'ast>,
state: &mut State<'ast, '_>,
ctxt: &Context<'ast>,
) -> Result<(), RowUnifError> {
) -> Result<(), RowUnifError<'ast>> {
let uerows1 = self.into_root(state.table);
let uerows2 = uerows2.into_root(state.table);

Expand Down Expand Up @@ -1476,14 +1479,14 @@ impl<'ast> Unify<'ast> for UnifEnumRows<'ast> {
}

impl<'ast> Unify<'ast> for UnifRecordRows<'ast> {
type Error = RowUnifError;
type Error = RowUnifError<'ast>;

fn unify(
self,
urrows2: UnifRecordRows<'ast>,
state: &mut State<'ast, '_>,
ctxt: &Context<'ast>,
) -> Result<(), RowUnifError> {
) -> Result<(), RowUnifError<'ast>> {
let urrows1 = self.into_root(state.table);
let urrows2 = urrows2.into_root(state.table);

Expand Down
48 changes: 26 additions & 22 deletions core/src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
//! Dually, the frontend is the user-facing part, which may be a CLI, a web application, a
//! jupyter-kernel (which is not exactly user-facing, but still manages input/output and
//! formatting), etc.
use crate::cache::{Cache, Envs, ErrorTolerance, InputFormat, SourcePath};
use crate::error::NullReporter;
use crate::error::{
report::{self, ColorOpt, ErrorFormat},
Error, EvalError, IOError, IntoDiagnostics, ParseError, ParseErrors, ReplError,
use crate::{
cache::{Cache, Envs, ErrorTolerance, InputFormat, SourcePath},
error::{
report::{self, ColorOpt, ErrorFormat},
Error, EvalError, IOError, IntoDiagnostics, NullReporter, ParseError, ParseErrors,
ReplError,
},
eval::{self, cache::Cache as EvalCache, Closure, VirtualMachine},
files::FileId,
identifier::LocIdent,
parser::{grammar, lexer, ErrorTolerantParserCompat, ExtendedTerm},
program::FieldPath,
term::{record::Field, RichTerm, Term},
transform::{self, import_resolution},
traverse::{Traverse, TraverseOrder},
typ::Type,
typecheck::{self, TypecheckMode},
};
use crate::eval::cache::Cache as EvalCache;
use crate::eval::{Closure, VirtualMachine};
use crate::files::FileId;
use crate::identifier::LocIdent;
use crate::parser::{grammar, lexer, ErrorTolerantParserCompat, ExtendedTerm};
use crate::program::FieldPath;
use crate::term::TraverseOrder;
use crate::term::{record::Field, RichTerm, Term, Traverse};
use crate::transform::import_resolution;
use crate::typ::Type;
use crate::typecheck::TypecheckMode;
use crate::{eval, transform, typecheck};

use simple_counter::*;
use std::convert::Infallible;
use std::ffi::{OsStr, OsString};
use std::io::Write;
use std::result::Result;
use std::str::FromStr;

use std::{
convert::Infallible,
ffi::{OsStr, OsString},
io::Write,
result::Result,
str::FromStr,
};

#[cfg(feature = "repl")]
use ansi_term::{Colour, Style};
Expand Down
1 change: 1 addition & 0 deletions core/src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
position::{RawSpan, TermPos},
pretty::PrettyPrintCap,
traverse::*,
traverse::{Traverse, TraverseControl, TraverseOrder},
typ::{Type, UnboundTypeVariableError},
typecheck::eq::{contract_eq, type_eq_noenv},
};
Expand Down
9 changes: 6 additions & 3 deletions core/src/transform/import_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ pub mod strict {
/// together with a (partially) resolved term.
pub mod tolerant {
use super::ImportResolver;
use crate::error::ImportError;
use crate::files::FileId;
use crate::term::{RichTerm, Term, Traverse, TraverseOrder};
use crate::{
error::ImportError,
files::FileId,
term::{RichTerm, Term},
traverse::{Traverse, TraverseOrder},
};

/// The result of an error tolerant import resolution.
#[derive(Debug)]
Expand Down
3 changes: 2 additions & 1 deletion core/src/transform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Program transformations.
use crate::{
cache::ImportResolver,
term::{RichTerm, Traverse, TraverseOrder},
term::RichTerm,
traverse::{Traverse, TraverseOrder},
typ::UnboundTypeVariableError,
typecheck::Wildcards,
};
Expand Down
3 changes: 2 additions & 1 deletion core/src/transform/substitute_wildcards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use crate::{
match_sharedterm,
term::{
record::{Field, FieldMetadata, RecordData},
LabeledType, RichTerm, Term, Traverse, TraverseOrder, TypeAnnotation,
LabeledType, RichTerm, Term, TypeAnnotation,
},
traverse::{Traverse, TraverseOrder},
typ::{Type, TypeF},
typecheck::Wildcards,
};
Expand Down
2 changes: 1 addition & 1 deletion core/src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,5 @@ where
.map(|elt| elt.traverse(alloc, f, order))
.collect();

Ok(alloc.alloc_many(collected))
Ok(alloc.alloc_many(collected?))
}

0 comments on commit 6b7ecda

Please sign in to comment.