Remove broken compile-time overload system (#9677)

# Description

This PR removes the compile-time overload system. Unfortunately, this
system never worked correctly because in a gradual type system where
types can be `Any`, you don't have enough information to correctly
resolve function calls with overloads. These resolutions must be done at
runtime, if they're supported.

That said, there's a bit of work that needs to go into resolving
input/output types (here overloads do not execute separate commands, but
the same command and each overload explains how each output type
corresponds to input types).

This PR also removes the type scope, which would give incorrect answers
in cases where multiple subexpressions were used in a pipeline.

# User-Facing Changes

Finishes removing compile-time overloads. These were only used in a few
places in the code base, but it's possible it may impact user code. I'll
mark this as breaking change so we can review.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
JT
2023-07-14 07:05:03 +12:00
committed by GitHub
parent 99329f14a3
commit 30904bd095
7 changed files with 118 additions and 445 deletions

View File

@ -815,8 +815,6 @@ pub fn parse_internal_call(
}
}
working_set.type_scope.add_type(output.clone());
if signature.creates_scope {
working_set.enter_scope();
}
@ -1052,8 +1050,7 @@ pub fn parse_call(
pos += 1;
}
let input = working_set.type_scope.get_previous();
let mut maybe_decl_id = working_set.find_decl(&name, input);
let mut maybe_decl_id = working_set.find_decl(&name);
while maybe_decl_id.is_none() {
// Find the longest command match
@ -1075,7 +1072,7 @@ pub fn parse_call(
name.extend(name_part);
}
}
maybe_decl_id = working_set.find_decl(&name, input);
maybe_decl_id = working_set.find_decl(&name);
}
if let Some(decl_id) = maybe_decl_id {
@ -1096,7 +1093,7 @@ pub fn parse_call(
}
// TODO: Try to remove the clone
let decl = working_set.get_decl(decl_id).clone();
let decl = working_set.get_decl(decl_id);
let parsed_call = if let Some(alias) = decl.as_alias() {
if let Expression {
@ -1104,7 +1101,7 @@ pub fn parse_call(
span: _,
ty,
custom_completion,
} = &alias.wrapped_call
} = &alias.clone().wrapped_call
{
trace!("parsing: alias of external call");
@ -2004,9 +2001,6 @@ pub fn parse_full_cell_path(
// Creating a Type scope to parse the new block. This will keep track of
// the previous input type found in that block
let output = parse_block(working_set, &output, span, true, true);
working_set
.type_scope
.add_type(working_set.type_scope.get_last_output());
let ty = output.output_type();
@ -2742,7 +2736,7 @@ pub fn parse_shape_name(
return SyntaxShape::Any;
}
let decl_id = working_set.find_decl(command_name, &Type::Any);
let decl_id = working_set.find_decl(command_name);
if let Some(decl_id) = decl_id {
return SyntaxShape::Custom(Box::new(shape), decl_id);
@ -5088,7 +5082,7 @@ pub fn parse_expression(
}
};
let with_env = working_set.find_decl(b"with-env", &Type::Any);
let with_env = working_set.find_decl(b"with-env");
if !shorthand.is_empty() {
if let Some(decl_id) = with_env {
@ -5146,14 +5140,7 @@ pub fn parse_variable(working_set: &mut StateWorkingSet, span: Span) -> Option<V
let bytes = working_set.get_span_contents(span);
if is_variable(bytes) {
if let Some(var_id) = working_set.find_variable(bytes) {
let input = working_set.get_variable(var_id).ty.clone();
working_set.type_scope.add_type(input);
Some(var_id)
} else {
None
}
working_set.find_variable(bytes)
} else {
working_set.error(ParseError::Expected("valid variable name", span));
@ -5172,7 +5159,7 @@ pub fn parse_builtin_commands(
{
trace!("parsing: not math expression or unaliasable parser keyword");
let name = working_set.get_span_contents(lite_command.parts[0]);
if let Some(decl_id) = working_set.find_decl(name, &Type::Nothing) {
if let Some(decl_id) = working_set.find_decl(name) {
let cmd = working_set.get_decl(decl_id);
if cmd.is_alias() {
// Parse keywords that can be aliased. Note that we check for "unaliasable" keywords
@ -5350,8 +5337,8 @@ pub fn parse_pipeline(
parse_builtin_commands(working_set, &new_command, is_subexpression);
if pipeline_index == 0 {
let let_decl_id = working_set.find_decl(b"let", &Type::Nothing);
let mut_decl_id = working_set.find_decl(b"mut", &Type::Nothing);
let let_decl_id = working_set.find_decl(b"let");
let mut_decl_id = working_set.find_decl(b"mut");
for element in pipeline.elements.iter_mut() {
if let PipelineElement::Expression(
_,
@ -5416,7 +5403,6 @@ pub fn parse_pipeline(
LiteElement::Command(span, command) => {
trace!("parsing: pipeline element: command");
let expr = parse_expression(working_set, &command.parts, is_subexpression);
working_set.type_scope.add_type(expr.ty.clone());
PipelineElement::Expression(*span, expr)
}
@ -5424,8 +5410,6 @@ pub fn parse_pipeline(
trace!("parsing: pipeline element: redirection");
let expr = parse_string(working_set, command.parts[0]);
working_set.type_scope.add_type(expr.ty.clone());
PipelineElement::Redirection(*span, redirection.clone(), expr)
}
LiteElement::SeparateRedirection {
@ -5435,12 +5419,8 @@ pub fn parse_pipeline(
trace!("parsing: pipeline element: separate redirection");
let out_expr = parse_string(working_set, out_command.parts[0]);
working_set.type_scope.add_type(out_expr.ty.clone());
let err_expr = parse_string(working_set, err_command.parts[0]);
working_set.type_scope.add_type(err_expr.ty.clone());
PipelineElement::SeparateRedirection {
out: (*out_span, out_expr),
err: (*err_span, err_expr),
@ -5452,9 +5432,7 @@ pub fn parse_pipeline(
} => {
trace!("parsing: pipeline element: same target redirection");
let expr = parse_expression(working_set, &command.parts, is_subexpression);
working_set.type_scope.add_type(expr.ty.clone());
let redirect_expr = parse_string(working_set, redirect_command.parts[0]);
working_set.type_scope.add_type(redirect_expr.ty.clone());
PipelineElement::SameTargetRedirection {
cmd: (*cmd_span, expr),
redirection: (*redirect_span, redirect_expr),
@ -5487,8 +5465,8 @@ pub fn parse_pipeline(
} => {
let mut pipeline = parse_builtin_commands(working_set, command, is_subexpression);
let let_decl_id = working_set.find_decl(b"let", &Type::Nothing);
let mut_decl_id = working_set.find_decl(b"mut", &Type::Nothing);
let let_decl_id = working_set.find_decl(b"let");
let mut_decl_id = working_set.find_decl(b"mut");
if pipeline_index == 0 {
for element in pipeline.elements.iter_mut() {
@ -5542,12 +5520,9 @@ pub fn parse_pipeline(
} => {
trace!("parsing: pipeline element: same target redirection");
let expr = parse_expression(working_set, &command.parts, is_subexpression);
working_set.type_scope.add_type(expr.ty.clone());
let redirect_expr = parse_string(working_set, redirect_cmd.parts[0]);
working_set.type_scope.add_type(redirect_expr.ty.clone());
Pipeline {
elements: vec![PipelineElement::SameTargetRedirection {
cmd: (*span, expr),
@ -5576,7 +5551,6 @@ pub fn parse_block(
if scoped {
working_set.enter_scope();
}
working_set.type_scope.enter_scope();
// Pre-declare any definition so that definitions
// that share the same block can see each other
@ -5605,7 +5579,6 @@ pub fn parse_block(
if scoped {
working_set.exit_scope();
}
working_set.type_scope.exit_scope();
block.span = Some(span);
@ -6008,7 +5981,7 @@ fn wrap_element_with_collect(
fn wrap_expr_with_collect(working_set: &mut StateWorkingSet, expr: &Expression) -> Expression {
let span = expr.span;
if let Some(decl_id) = working_set.find_decl(b"collect", &Type::List(Box::new(Type::Any))) {
if let Some(decl_id) = working_set.find_decl(b"collect") {
let mut output = vec![];
let var_id = IN_VARIABLE_ID;