Skip to content

Commit

Permalink
[Refactor] Types -> Type, types -> typ (#1472)
Browse files Browse the repository at this point in the history
* Types -> Type, types -> typ

`type` is a reserved keyword in Rust, which is why we've been adding an
`s` to uppercase `Type` and lowercase as in `crate::types::Types`.
However:
1. uppercase `Type` is actually valid, only lowercase `type` clashes
   with the Rust keyword.
2. Adding an `s` has caused confusion, because there's no easy way to
   tell if a struct field `types` means there are several types, or if
   it's just a work-around to avoid the keyword.

In consequence, this commits rename lowercase `types` to `typ` (and in
particular the module `types` is now `typ`), and the uppercase `Types`
to `Type` (so, previously `types::Types` is now `type::Type`).

* Formatting
  • Loading branch information
yannham authored Jul 25, 2023
1 parent 835f15c commit 498e91d
Show file tree
Hide file tree
Showing 39 changed files with 627 additions and 639 deletions.
6 changes: 3 additions & 3 deletions cli/bin/nickel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enum Command {
#[arg(long)]
contract: bool,
#[arg(long = "type")]
types: bool,
typ: bool,
#[arg(long)]
default: bool,
#[arg(long)]
Expand Down Expand Up @@ -135,7 +135,7 @@ fn handle_eval_commands(opts: Opt) {
path,
doc,
contract,
types,
typ: types,
default,
value,
}) => {
Expand All @@ -147,7 +147,7 @@ fn handle_eval_commands(opts: Opt) {
query_print::Attributes {
doc,
contract,
types,
typ: types,
default,
value,
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::stdlib::{self as nickel_stdlib, StdlibModule};
use crate::term::record::{Field, RecordData};
use crate::term::{RichTerm, SharedTerm, Term};
use crate::transform::import_resolution;
use crate::typ::UnboundTypeVariableError;
use crate::typecheck::{self, type_check, Wildcards};
use crate::types::UnboundTypeVariableError;
use crate::{eval, parser, transform};
use codespan::{FileId, Files};
use io::Read;
Expand Down
6 changes: 3 additions & 3 deletions core/src/destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
record::{Field, RecordAttrs, RecordData},
LabeledType, Term,
},
types::{TypeF, Types},
typ::{Type, TypeF},
};

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -121,8 +121,8 @@ impl RecordPattern {
let is_open = self.is_open();
let pos = TermPos::Original(self.span);
LabeledType {
types: Types {
types: TypeF::Flat(
typ: Type {
typ: TypeF::Flat(
Term::Record(RecordData::new(
self.inner().into_iter().map(Match::as_binding).collect(),
RecordAttrs { open: is_open },
Expand Down
56 changes: 28 additions & 28 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
repl,
serialize::ExportFormat,
term::{record::FieldMetadata, Number, RichTerm},
types::{TypeF, Types, VarKindDiscriminant},
typ::{Type, TypeF, VarKindDiscriminant},
};

/// A general error occurring during either parsing or evaluation.
Expand Down Expand Up @@ -184,27 +184,27 @@ pub enum TypecheckError {
/// A specific row was expected to be in the type of an expression, but was not.
MissingRow(
Ident,
/* the expected type */ Types,
/* the inferred/annotated type */ Types,
/* the expected type */ Type,
/* the inferred/annotated type */ Type,
TermPos,
),
/// A dynamic tail was expected to be in the type of an expression, but was not.
MissingDynTail(
/* the expected type */ Types,
/* the inferred/annotated type */ Types,
/* the expected type */ Type,
/* the inferred/annotated type */ Type,
TermPos,
),
/// A specific row was not expected to be in the type of an expression.
ExtraRow(
Ident,
/* the expected type */ Types,
/* the inferred/annotated type */ Types,
/* the expected type */ Type,
/* the inferred/annotated type */ Type,
TermPos,
),
/// A additional dynamic tail was not expected to be in the type of an expression.
ExtraDynTail(
/* the expected type */ Types,
/* the inferred/annotated type */ Types,
/* the expected type */ Type,
/* the inferred/annotated type */ Type,
TermPos,
),
/// A parametricity violation involving a row-kinded type variable.
Expand All @@ -221,24 +221,24 @@ pub enum TypecheckError {
/// `{ y : String }` as the `violating_type`.
ForallParametricityViolation {
kind: VarKindDiscriminant,
tail: Types,
violating_type: Types,
tail: Type,
violating_type: Type,
pos: TermPos,
},
/// An unbound type variable was referenced.
UnboundTypeVariable(Ident),
/// The actual (inferred or annotated) type of an expression is incompatible with its expected
/// type.
TypeMismatch(
/* the expected type */ Types,
/* the actual type */ Types,
/* the expected type */ Type,
/* the actual type */ Type,
TermPos,
),
/// Two incompatible kind (enum vs record) have been deduced for the same identifier of a row type.
RowMismatch(
Ident,
/* the expected row type (whole) */ Types,
/* the actual row type (whole) */ Types,
/* the expected row type (whole) */ Type,
/* the actual row type (whole) */ Type,
/* error at the given row */ Box<TypecheckError>,
TermPos,
),
Expand All @@ -257,9 +257,9 @@ pub enum TypecheckError {
/// the direct failure to unify `{ .. , x: T1, .. }` and `{ .., x: T2, .. }`.
RowConflict(
Ident,
/* the second type assignment which violates the constraint */ Types,
/* the expected type of the subexpression */ Types,
/* the actual type of the subexpression */ Types,
/* the second type assignment which violates the constraint */ Type,
/* the expected type of the subexpression */ Type,
/* the actual type of the subexpression */ Type,
TermPos,
),
/// Type mismatch on a subtype of an an arrow type.
Expand All @@ -278,8 +278,8 @@ pub enum TypecheckError {
/// This specific error stores additionally the [type path][crate::label::ty_path] that
/// identifies the subtype where unification failed and the corresponding error.
ArrowTypeMismatch(
/* the expected arrow type */ Types,
/* the actual arrow type */ Types,
/* the expected arrow type */ Type,
/* the actual arrow type */ Type,
/* the path to the incompatible subtypes */ ty_path::Path,
/* the error on the subtype unification */ Box<TypecheckError>,
TermPos,
Expand Down Expand Up @@ -1277,7 +1277,7 @@ mod blame_error {
},
position::TermPos,
term::RichTerm,
types::Types,
typ::Type,
};

use super::{primary, secondary, secondary_term};
Expand Down Expand Up @@ -1427,7 +1427,7 @@ mod blame_error {
/// subtype isn't defined), [path_span] pretty-prints the type inside a new source, parses it,
/// and calls `ty_path::span`. This new type is guaranteed to have all of its positions set,
/// providing a definite `PathSpan`. This is similar to the behavior of [`super::primary_alt`].
pub fn path_span(files: &mut Files<String>, path: &[ty_path::Elem], ty: &Types) -> PathSpan {
pub fn path_span(files: &mut Files<String>, path: &[ty_path::Elem], ty: &Type) -> PathSpan {
use crate::parser::{grammar::FixedTypeParser, lexer::Lexer, ErrorTolerantParser};

ty_path::span(path.iter().peekable(), ty).or_else(|| {
Expand Down Expand Up @@ -1457,7 +1457,7 @@ mod blame_error {
span,
last,
last_arrow_elem,
} = path_span(files, &l.path, &l.types);
} = path_span(files, &l.path, &l.typ);

let (msg, notes) = match (last, last_arrow_elem) {
// The type path doesn't contain any arrow, and the failing subcontract is the
Expand Down Expand Up @@ -1920,14 +1920,14 @@ impl IntoDiagnostics<FileId> for TypecheckError {
"Did you forget to put a `forall {ident}.` somewhere in the enclosing type?"
)])],
TypecheckError::TypeMismatch(expd, actual, span_opt) => {
fn addendum(ty: &Types) -> &str {
if ty.types.is_flat() {
fn addendum(ty: &Type) -> &str {
if ty.typ.is_flat() {
" (a contract)"
} else {
""
}
}
let last_note = if expd.types.is_flat() ^ actual.types.is_flat() {
let last_note = if expd.typ.is_flat() ^ actual.typ.is_flat() {
"Static types and contracts are not compatible"
} else {
"These types are not compatible"
Expand Down Expand Up @@ -1971,7 +1971,7 @@ impl IntoDiagnostics<FileId> for TypecheckError {
format!("Found an expression of a record type with the row `{field}: {ty}`")
};

let note1 = if let TypeF::Record(rrows) = &expd.types {
let note1 = if let TypeF::Record(rrows) = &expd.typ {
match rrows.row_find_path(path.as_slice()) {
Some(ty) => mk_expected_row_msg(&field, ty),
None => mk_expected_msg(&expd),
Expand All @@ -1980,7 +1980,7 @@ impl IntoDiagnostics<FileId> for TypecheckError {
mk_expected_msg(&expd)
};

let note2 = if let TypeF::Record(rrows) = &actual.types {
let note2 = if let TypeF::Record(rrows) = &actual.typ {
match rrows.row_find_path(path.as_slice()) {
Some(ty) => mk_inferred_row_msg(&field, ty),
None => mk_inferred_msg(&actual),
Expand Down
8 changes: 4 additions & 4 deletions core/src/eval/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub fn merge<C: Cache>(
// `array1 & array2` to `contract.Equal array1 array2`, so that we extend merge in the
// minimum way such that it is idempotent.
(t1 @ Term::Array(..), t2 @ Term::Array(..)) => {
use crate::{mk_app, stdlib, types::TypeF};
use crate::{mk_app, stdlib, typ::TypeF};
use std::rc::Rc;

let mut env = Environment::new();
Expand All @@ -189,7 +189,7 @@ pub fn merge<C: Cache>(
);

let label = Label {
types: Rc::new(TypeF::Flat(contract_for_display).into()),
typ: Rc::new(TypeF::Flat(contract_for_display).into()),
span: MergeLabel::from(mode).span,
..Default::default()
}
Expand Down Expand Up @@ -417,7 +417,7 @@ fn merge_fields<'a, C: Cache, I: DoubleEndedIterator<Item = &'a Ident> + Clone>(

// If both have type annotations, we arbitrarily choose the first one as the type annotation
// for the resulting field. This doesn't make any difference operationally.
let types = match (annot1.types.take(), annot2.types.take()) {
let typ = match (annot1.typ.take(), annot2.typ.take()) {
(Some(ctr1), Some(ctr2)) => {
annot1.contracts.push(ctr2);
Some(ctr1)
Expand All @@ -433,7 +433,7 @@ fn merge_fields<'a, C: Cache, I: DoubleEndedIterator<Item = &'a Ident> + Clone>(

let metadata = FieldMetadata {
doc: merge_doc(metadata1.doc, metadata2.doc),
annotation: TypeAnnotation { types, contracts },
annotation: TypeAnnotation { typ, contracts },
// If one of the record requires this field, then it musn't be optional. The
// resulting field is optional iff both are.
opt: metadata1.opt && metadata2.opt,
Expand Down
4 changes: 2 additions & 2 deletions core/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
}
}
// Evaluating a type turns it into a contract.
Term::Types(ty) => Closure {
Term::Type(ty) => Closure {
body: ty.contract()?,
env,
},
Expand Down Expand Up @@ -969,7 +969,7 @@ pub fn subst<C: Cache>(
// We could recurse here, because types can contain terms which would then be subject to
// substitution. Not recursing should be fine, though, because a type in term position
// turns into a contract, and we don't substitute inside contracts either currently.
| v @ Term::Types(_) => RichTerm::new(v, pos),
| v @ Term::Type(_) => RichTerm::new(v, pos),
Term::Let(id, t1, t2, attrs) => {
let t1 = subst(cache, t1, initial_env, env);
let t2 = subst(cache, t2, initial_env, env);
Expand Down
22 changes: 11 additions & 11 deletions core/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
record::{Field, RecordData},
RichTerm, SealingKey, Term,
},
typ::{Type, TypeF},
typecheck::{ReifyAsUnifType, UnifType},
types::{TypeF, Types},
};

use codespan::Files;
Expand Down Expand Up @@ -55,7 +55,7 @@ pub mod ty_path {
use crate::{
identifier::Ident,
position::RawSpan,
types::{RecordRowF, RecordRowsIteratorItem, TypeF, Types},
typ::{RecordRowF, RecordRowsIteratorItem, Type, TypeF},
};

/// An element of a path type.
Expand Down Expand Up @@ -141,16 +141,16 @@ pub mod ty_path {
/// Here, the type path will contain an `Array` (added by the builtin implementation of the
/// `Array` contract), but the original type will be `Foo`, which isn't of the form `Array _`.
/// Thus we can't underline the subtype `_`, and stops at the whole `Array T`.
pub fn span<'a, I>(mut path_it: std::iter::Peekable<I>, mut ty: &Types) -> Option<PathSpan>
pub fn span<'a, I>(mut path_it: std::iter::Peekable<I>, mut ty: &Type) -> Option<PathSpan>
where
I: Iterator<Item = &'a Elem>,
I: std::clone::Clone,
{
while let TypeF::Forall { body, .. } = &ty.types {
while let TypeF::Forall { body, .. } = &ty.typ {
ty = body.as_ref();
}

match (&ty.types, path_it.next()) {
match (&ty.typ, path_it.next()) {
(TypeF::Arrow(dom, codom), Some(next)) => {
match next {
Elem::Domain => {
Expand All @@ -175,7 +175,7 @@ pub mod ty_path {
(TypeF::Record(rows), next @ Some(Elem::Field(ident))) => {
for row_item in rows.iter() {
match row_item {
RecordRowsIteratorItem::Row(RecordRowF { id, types: ty })
RecordRowsIteratorItem::Row(RecordRowF { id, typ: ty })
if id == *ident =>
{
let path_span = span(path_it, ty)?;
Expand All @@ -195,10 +195,10 @@ pub mod ty_path {
"span: current type path element indicates to go to field `{}`,\
but this field doesn't exist in {}",
ident,
Types::from(TypeF::Record(rows.clone())),
Type::from(TypeF::Record(rows.clone())),
)
}
(TypeF::Array(ty), Some(Elem::Array)) if ty.as_ref().types == TypeF::Dyn =>
(TypeF::Array(ty), Some(Elem::Array)) if ty.as_ref().typ == TypeF::Dyn =>
// Dyn shouldn't be the target of any blame
{
panic!("span(): unexpected blame of a dyn contract inside an array")
Expand Down Expand Up @@ -275,7 +275,7 @@ but this field doesn't exist in {}",
#[derive(Debug, Clone, PartialEq)]
pub struct Label {
/// The type checked by the original contract.
pub types: Rc<Types>,
pub typ: Rc<Type>,
/// Custom diagnostics set by user code. There might be several diagnostics stacked up, as some
/// contracts might in turn apply other subcontracts.
///
Expand Down Expand Up @@ -418,7 +418,7 @@ impl Label {
/// Generate a dummy label for testing purpose.
pub fn dummy() -> Label {
Label {
types: Rc::new(Types::from(TypeF::Number)),
typ: Rc::new(Type::from(TypeF::Number)),
diagnostics: vec![ContractDiagnostic::new().with_message(String::from("testing"))],
span: RawSpan {
src_id: Files::new().add("<test>", String::from("empty")),
Expand Down Expand Up @@ -502,7 +502,7 @@ impl Label {
impl Default for Label {
fn default() -> Label {
Label {
types: Rc::new(Types::from(TypeF::Dyn)),
typ: Rc::new(Type::from(TypeF::Dyn)),
span: RawSpan {
src_id: Files::new().add("<null>", String::from("")),
start: 0.into(),
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ pub mod serialize;
pub mod stdlib;
pub mod term;
pub mod transform;
pub mod typ;
pub mod typecheck;
pub mod types;
Loading

0 comments on commit 498e91d

Please sign in to comment.