mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 05:14:44 +02:00
allow records to have type annotations (#8914)
# Description follow up to #8529 cleaned up version of #8892 - the original syntax is okay ```nu def okay [rec: record] {} ``` - you can now add type annotations for fields if you know them before hand ```nu def okay [rec: record<name: string>] {} ``` - you can specify multiple fields ```nu def okay [person: record<name: string age: int>] {} # an optional comma is allowed def okay [person: record<name: string, age: int>] {} ``` - if annotations are specified, any use of the command will be type checked against the specified type ```nu def unwrap [result: record<ok: bool, value: any>] {} unwrap {ok: 2, value: "value"} # errors with Error: nu::parser::type_mismatch × Type mismatch. ╭─[entry #4:1:1] 1 │ unwrap {ok: 2, value: "value"} · ───────┬───── · ╰── expected record<ok: bool, value: any>, found record<ok: int, value: string> ╰──── ``` > here the error is in the `ok` field, since `any` is coerced into any type > as a result `unwrap {ok: true, value: "value"}` is okay - the key must be a string, either quoted or unquoted ```nu def err [rec: record<{}: list>] {} # errors with Error: × `record` type annotations key not string ╭─[entry #7:1:1] 1 │ def unwrap [result: record<{}: bool, value: any>] {} · ─┬ · ╰── must be a string ╰──── ``` - a key doesn't have to have a type in which case it is assumed to be `any` ```nu def okay [person: record<name age>] {} def okay [person: record<name: string age>] {} ``` - however, if you put a colon, you have to specify a type ```nu def err [person: record<name: >] {} # errors with Error: nu::parser::parse_mismatch × Parse mismatch during operation. ╭─[entry #12:1:1] 1 │ def unwrap [res: record<name: >] { $res } · ┬ · ╰── expected type after colon ╰──── ``` # User-Facing Changes **[BREAKING CHANGES]** - this change adds a field to `SyntaxShape::Record` so any plugins that used it will have to update and include the field. though if you are unsure of the type the record expects, `SyntaxShape::Record(vec![])` will suffice
This commit is contained in:
@ -2775,7 +2775,7 @@ pub fn parse_shape_name(
|
||||
b"operator" => SyntaxShape::Operator,
|
||||
b"path" => SyntaxShape::Filepath,
|
||||
b"range" => SyntaxShape::Range,
|
||||
b"record" => SyntaxShape::Record,
|
||||
_ if bytes.starts_with(b"record") => parse_collection_shape(working_set, bytes, span),
|
||||
b"signature" => SyntaxShape::Signature,
|
||||
b"string" => SyntaxShape::String,
|
||||
b"table" => SyntaxShape::Table,
|
||||
@ -2814,38 +2814,129 @@ pub fn parse_shape_name(
|
||||
result
|
||||
}
|
||||
|
||||
fn parse_collection_shape(
|
||||
working_set: &mut StateWorkingSet,
|
||||
bytes: &[u8],
|
||||
span: Span,
|
||||
) -> SyntaxShape {
|
||||
assert!(bytes.starts_with(b"record"));
|
||||
let name = "record";
|
||||
let mk_shape = SyntaxShape::Record;
|
||||
|
||||
if bytes == name.as_bytes() {
|
||||
mk_shape(vec![])
|
||||
} else if bytes.starts_with(b"record<") {
|
||||
let Some(inner_span) = prepare_inner_span(working_set, bytes, span, 7) else {
|
||||
return SyntaxShape::Any;
|
||||
};
|
||||
|
||||
// record<> or table<>
|
||||
if inner_span.end - inner_span.start == 0 {
|
||||
return mk_shape(vec![]);
|
||||
}
|
||||
let source = working_set.get_span_contents(inner_span);
|
||||
let (tokens, err) = lex_signature(
|
||||
source,
|
||||
inner_span.start,
|
||||
&[b'\n', b'\r'],
|
||||
&[b':', b','],
|
||||
true,
|
||||
);
|
||||
|
||||
if let Some(err) = err {
|
||||
working_set.error(err);
|
||||
// lexer errors cause issues with span overflows
|
||||
return mk_shape(vec![]);
|
||||
}
|
||||
|
||||
let mut sig = vec![];
|
||||
let mut idx = 0;
|
||||
|
||||
let key_error = |span| {
|
||||
ParseError::LabeledError(
|
||||
format!("`{name}` type annotations key not string"),
|
||||
"must be a string".into(),
|
||||
span,
|
||||
)
|
||||
};
|
||||
|
||||
while idx < tokens.len() {
|
||||
let TokenContents::Item = tokens[idx].contents else {
|
||||
working_set.error(key_error(tokens[idx].span));
|
||||
return mk_shape(vec![])
|
||||
};
|
||||
|
||||
let key_bytes = working_set.get_span_contents(tokens[idx].span).to_vec();
|
||||
if key_bytes.first().copied() == Some(b',') {
|
||||
idx += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
let Some(key) = parse_value(working_set, tokens[idx].span, &SyntaxShape::String).as_string() else {
|
||||
working_set.error(key_error(tokens[idx].span));
|
||||
return mk_shape(vec![]);
|
||||
};
|
||||
|
||||
// we want to allow such an annotation
|
||||
// `record<name>` where the user leaves out the type
|
||||
if idx + 1 == tokens.len() {
|
||||
sig.push((key, SyntaxShape::Any));
|
||||
break;
|
||||
} else {
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
let maybe_colon = working_set.get_span_contents(tokens[idx].span).to_vec();
|
||||
match maybe_colon.as_slice() {
|
||||
b":" => {
|
||||
if idx + 1 == tokens.len() {
|
||||
working_set.error(ParseError::Expected(
|
||||
"type after colon".into(),
|
||||
tokens[idx].span,
|
||||
));
|
||||
break;
|
||||
} else {
|
||||
idx += 1;
|
||||
}
|
||||
}
|
||||
// a key provided without a type
|
||||
b"," => {
|
||||
idx += 1;
|
||||
sig.push((key, SyntaxShape::Any));
|
||||
continue;
|
||||
}
|
||||
// a key provided without a type
|
||||
_ => {
|
||||
sig.push((key, SyntaxShape::Any));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let shape_bytes = working_set.get_span_contents(tokens[idx].span).to_vec();
|
||||
let shape = parse_shape_name(working_set, &shape_bytes, tokens[idx].span);
|
||||
sig.push((key, shape));
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
mk_shape(sig)
|
||||
} else {
|
||||
working_set.error(ParseError::UnknownType(span));
|
||||
|
||||
SyntaxShape::Any
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_list_shape(working_set: &mut StateWorkingSet, bytes: &[u8], span: Span) -> SyntaxShape {
|
||||
assert!(bytes.starts_with(b"list"));
|
||||
|
||||
if bytes == b"list" {
|
||||
SyntaxShape::List(Box::new(SyntaxShape::Any))
|
||||
} else if bytes.starts_with(b"list<") {
|
||||
let start = span.start + 5;
|
||||
|
||||
// if the annotation is unterminated, we want to return early to avoid
|
||||
// overflows with spans
|
||||
let end = if bytes.ends_with(b">") {
|
||||
span.end - 1
|
||||
// extra characters after the >
|
||||
} else if bytes.contains(&b'>') {
|
||||
let angle_start = bytes.split(|it| it == &b'>').collect::<Vec<_>>()[0].len() + 1;
|
||||
let span = Span::new(span.start + angle_start, span.end);
|
||||
|
||||
working_set.error(ParseError::LabeledError(
|
||||
"Extra characters in the parameter name".into(),
|
||||
"extra characters".into(),
|
||||
span,
|
||||
));
|
||||
let Some(inner_span) = prepare_inner_span(working_set, bytes, span, 5) else {
|
||||
return SyntaxShape::Any;
|
||||
} else {
|
||||
working_set.error(ParseError::Unclosed(">".into(), span));
|
||||
return SyntaxShape::List(Box::new(SyntaxShape::Any));
|
||||
};
|
||||
|
||||
let inner_span = Span::new(start, end);
|
||||
|
||||
let inner_text = String::from_utf8_lossy(working_set.get_span_contents(inner_span));
|
||||
|
||||
// remove any extra whitespace, for example `list< string >` becomes `list<string>`
|
||||
let inner_bytes = inner_text.trim().as_bytes().to_vec();
|
||||
|
||||
@ -2864,6 +2955,34 @@ fn parse_list_shape(working_set: &mut StateWorkingSet, bytes: &[u8], span: Span)
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare_inner_span(
|
||||
working_set: &mut StateWorkingSet,
|
||||
bytes: &[u8],
|
||||
span: Span,
|
||||
prefix_len: usize,
|
||||
) -> Option<Span> {
|
||||
let start = span.start + prefix_len;
|
||||
|
||||
if bytes.ends_with(b">") {
|
||||
let end = span.end - 1;
|
||||
Some(Span::new(start, end))
|
||||
} else if bytes.contains(&b'>') {
|
||||
let angle_start = bytes.split(|it| it == &b'>').collect::<Vec<_>>()[0].len() + 1;
|
||||
let span = Span::new(span.start + angle_start, span.end);
|
||||
|
||||
working_set.error(ParseError::LabeledError(
|
||||
"Extra characters in the parameter name".into(),
|
||||
"extra characters".into(),
|
||||
span,
|
||||
));
|
||||
|
||||
None
|
||||
} else {
|
||||
working_set.error(ParseError::Unclosed(">".into(), span));
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_type(_working_set: &StateWorkingSet, bytes: &[u8]) -> Type {
|
||||
match bytes {
|
||||
b"binary" => Type::Binary,
|
||||
@ -3602,43 +3721,13 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) ->
|
||||
expression.ty.clone(),
|
||||
);
|
||||
}
|
||||
Type::List(param_ty) => {
|
||||
if let Type::List(expr_ty) = &expression.ty {
|
||||
if param_ty == expr_ty
|
||||
|| **param_ty == Type::Any
|
||||
{
|
||||
working_set.set_variable_type(
|
||||
var_id,
|
||||
expression.ty.clone(),
|
||||
);
|
||||
} else {
|
||||
working_set.error(ParseError::AssignmentMismatch(
|
||||
"Default value wrong type"
|
||||
.into(),
|
||||
format!(
|
||||
"expected default value to be `{var_type}`",
|
||||
),
|
||||
expression.span,
|
||||
),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
working_set.error(ParseError::AssignmentMismatch(
|
||||
"Default value wrong type".into(),
|
||||
format!(
|
||||
"expected default value to be `{var_type}`",
|
||||
),
|
||||
expression.span,
|
||||
))
|
||||
}
|
||||
}
|
||||
t => {
|
||||
if t != &expression.ty {
|
||||
_ => {
|
||||
if !type_compatible(var_type, &expression.ty) {
|
||||
working_set.error(
|
||||
ParseError::AssignmentMismatch(
|
||||
"Default value wrong type".into(),
|
||||
format!(
|
||||
"expected default value to be `{t}`"
|
||||
"expected default value to be `{var_type}`"
|
||||
),
|
||||
expression.span,
|
||||
),
|
||||
@ -3686,7 +3775,7 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) ->
|
||||
"Default value is the wrong type"
|
||||
.into(),
|
||||
format!(
|
||||
"default value should be {t}"
|
||||
"expected default value to be `{t}`"
|
||||
),
|
||||
expression_span,
|
||||
),
|
||||
@ -4485,7 +4574,7 @@ pub fn parse_value(
|
||||
|
||||
// Be sure to return ParseError::Expected(..) if invoked for one of these shapes, but lex
|
||||
// stream doesn't start with '{'} -- parsing in SyntaxShape::Any arm depends on this error variant.
|
||||
SyntaxShape::Block | SyntaxShape::Closure(..) | SyntaxShape::Record => {
|
||||
SyntaxShape::Block | SyntaxShape::Closure(..) | SyntaxShape::Record(_) => {
|
||||
working_set.error(ParseError::Expected(
|
||||
"block, closure or record".into(),
|
||||
span,
|
||||
@ -4505,7 +4594,7 @@ pub fn parse_value(
|
||||
SyntaxShape::Duration,
|
||||
SyntaxShape::Range,
|
||||
SyntaxShape::DateTime, //FIXME requires 3 failed conversion attempts before failing
|
||||
SyntaxShape::Record,
|
||||
SyntaxShape::Record(vec![]),
|
||||
SyntaxShape::Closure(None),
|
||||
SyntaxShape::Block,
|
||||
SyntaxShape::Int,
|
||||
|
@ -5,6 +5,21 @@ use nu_protocol::{
|
||||
};
|
||||
|
||||
pub fn type_compatible(lhs: &Type, rhs: &Type) -> bool {
|
||||
// Structural subtyping
|
||||
let is_compatible = |expected: &[(String, Type)], found: &[(String, Type)]| {
|
||||
// the expected type is `any`
|
||||
if expected.is_empty() {
|
||||
true
|
||||
} else if expected.len() != found.len() {
|
||||
false
|
||||
} else {
|
||||
expected
|
||||
.iter()
|
||||
.zip(found.iter())
|
||||
.all(|(lhs, rhs)| lhs.0 == rhs.0 && type_compatible(&lhs.1, &rhs.1))
|
||||
}
|
||||
};
|
||||
|
||||
match (lhs, rhs) {
|
||||
(Type::List(c), Type::List(d)) => type_compatible(c, d),
|
||||
(Type::Number, Type::Int) => true,
|
||||
@ -13,20 +28,7 @@ pub fn type_compatible(lhs: &Type, rhs: &Type) -> bool {
|
||||
(Type::Any, _) => true,
|
||||
(_, Type::Any) => true,
|
||||
(Type::Record(fields_lhs), Type::Record(fields_rhs)) => {
|
||||
// Structural subtyping
|
||||
'outer: for field_lhs in fields_lhs {
|
||||
for field_rhs in fields_rhs {
|
||||
if field_lhs.0 == field_rhs.0 {
|
||||
if type_compatible(&field_lhs.1, &field_rhs.1) {
|
||||
continue 'outer;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
true
|
||||
is_compatible(fields_lhs, fields_rhs)
|
||||
}
|
||||
(lhs, rhs) => lhs == rhs,
|
||||
}
|
||||
|
Reference in New Issue
Block a user