Skip to content

Commit 8ec66dc

Browse files
GearsDatapackslpil
authored andcommitted
Reword other code actions to prevent type variable duplication
1 parent 89f176d commit 8ec66dc

5 files changed

+83
-18
lines changed

compiler-core/src/language_server/code_action.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
type_::{
2222
self, FieldMap, ModuleValueConstructor, Type, TypeVar, TypedCallArg, ValueConstructor,
2323
error::{ModuleSuggestion, VariableDeclaration, VariableOrigin},
24-
printer::{Names, Printer},
24+
printer::Printer,
2525
},
2626
};
2727
use ecow::{EcoString, eco_format};
@@ -1310,7 +1310,7 @@ impl<'a> AddAnnotations<'a> {
13101310
edits: TextEdits::new(line_numbers),
13111311
// We need to use the same printer for all the edits because otherwise
13121312
// we could get duplicate type variable names.
1313-
printer: Printer::new(&module.ast.names),
1313+
printer: Printer::new_without_type_variables(&module.ast.names),
13141314
}
13151315
}
13161316

@@ -3545,7 +3545,9 @@ impl<'a> GenerateDynamicDecoder<'a> {
35453545
params: &'a CodeActionParams,
35463546
actions: &'a mut Vec<CodeAction>,
35473547
) -> Self {
3548-
let printer = Printer::new(&module.ast.names);
3548+
// Since we are generating a new function, type variables from other
3549+
// functions and constants are irrelevant to the types we print.
3550+
let printer = Printer::new_without_type_variables(&module.ast.names);
35493551
Self {
35503552
module,
35513553
params,
@@ -3633,7 +3635,7 @@ impl<'a> GenerateDynamicDecoder<'a> {
36333635
}
36343636

36353637
let mut decoder_printer = DecoderPrinter::new(
3636-
&self.module.ast.names,
3638+
&mut self.printer,
36373639
custom_type.name.clone(),
36383640
self.module.name.clone(),
36393641
);
@@ -3741,8 +3743,8 @@ fn maybe_import(edits: &mut TextEdits<'_>, module: &Module, module_name: &str) {
37413743
));
37423744
}
37433745

3744-
struct DecoderPrinter<'a> {
3745-
printer: Printer<'a>,
3746+
struct DecoderPrinter<'a, 'b> {
3747+
printer: &'a mut Printer<'b>,
37463748
/// The name of the root type we are printing a decoder for
37473749
type_name: EcoString,
37483750
/// The module name of the root type we are printing a decoder for
@@ -3792,12 +3794,12 @@ impl RecordLabel<'_> {
37923794
}
37933795
}
37943796

3795-
impl<'a> DecoderPrinter<'a> {
3796-
fn new(names: &'a Names, type_name: EcoString, type_module: EcoString) -> Self {
3797+
impl<'a, 'b> DecoderPrinter<'a, 'b> {
3798+
fn new(printer: &'a mut Printer<'b>, type_name: EcoString, type_module: EcoString) -> Self {
37973799
Self {
37983800
type_name,
37993801
type_module,
3800-
printer: Printer::new(names),
3802+
printer,
38013803
}
38023804
}
38033805

@@ -3937,7 +3939,9 @@ impl<'a> GenerateJsonEncoder<'a> {
39373939
actions: &'a mut Vec<CodeAction>,
39383940
config: &'a PackageConfig,
39393941
) -> Self {
3940-
let printer = Printer::new(&module.ast.names);
3942+
// Since we are generating a new function, type variables from other
3943+
// functions and constants are irrelevant to the types we print.
3944+
let printer = Printer::new_without_type_variables(&module.ast.names);
39413945
Self {
39423946
module,
39433947
params,
@@ -4041,7 +4045,7 @@ impl<'a> GenerateJsonEncoder<'a> {
40414045

40424046
// Otherwise we turn it into an object with a `type` tag field.
40434047
let mut encoder_printer =
4044-
JsonEncoderPrinter::new(&self.module.ast.names, type_name, self.module.name.clone());
4048+
JsonEncoderPrinter::new(&mut self.printer, type_name, self.module.name.clone());
40454049

40464050
// These are the fields of the json object to encode.
40474051
let mut fields = Vec::with_capacity(constructor.arguments.len());
@@ -4126,20 +4130,20 @@ fn {name}({record_name}: {type_}) -> {json_type} {{
41264130
}
41274131
}
41284132

4129-
struct JsonEncoderPrinter<'a> {
4130-
printer: Printer<'a>,
4133+
struct JsonEncoderPrinter<'a, 'b> {
4134+
printer: &'a mut Printer<'b>,
41314135
/// The name of the root type we are printing an encoder for
41324136
type_name: EcoString,
41334137
/// The module name of the root type we are printing an encoder for
41344138
type_module: EcoString,
41354139
}
41364140

4137-
impl<'a> JsonEncoderPrinter<'a> {
4138-
fn new(names: &'a Names, type_name: EcoString, type_module: EcoString) -> Self {
4141+
impl<'a, 'b> JsonEncoderPrinter<'a, 'b> {
4142+
fn new(printer: &'a mut Printer<'b>, type_name: EcoString, type_module: EcoString) -> Self {
41394143
Self {
41404144
type_name,
41414145
type_module,
4142-
printer: Printer::new(names),
4146+
printer,
41434147
}
41444148
}
41454149

@@ -4982,7 +4986,10 @@ impl<'a> GenerateFunction<'a> {
49824986
// generators to avoid renaming a label in case it shares a name with an argument.
49834987
let mut label_names = NameGenerator::new();
49844988
let mut argument_names = NameGenerator::new();
4985-
let mut printer = Printer::new(&self.module.ast.names);
4989+
4990+
// Since we are generating a new function, type variables from other
4991+
// functions and constants are irrelevant to the types we print.
4992+
let mut printer = Printer::new_without_type_variables(&self.module.ast.names);
49864993
let arguments = arguments_types
49874994
.iter()
49884995
.enumerate()

compiler-core/src/language_server/tests/action.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9524,3 +9524,21 @@ fn wibble(a, b, c) {
95249524
find_position_of("wibble").to_selection()
95259525
);
95269526
}
9527+
9528+
#[test]
9529+
fn generated_function_annotations_are_not_affected_by_other_functions() {
9530+
assert_code_action!(
9531+
GENERATE_FUNCTION,
9532+
"
9533+
fn wibble(a: a, b: b, c: c) -> d { todo }
9534+
9535+
pub fn main() {
9536+
let x = todo
9537+
let y = todo
9538+
let #(a, b) = something(x, y)
9539+
b
9540+
}
9541+
",
9542+
find_position_of("something").to_selection()
9543+
);
9544+
}

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__generate_function_capture.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ pub fn main() {
2424
map([1, 2, 3], add(_, 1))
2525
}
2626

27-
fn add(int: Int, int_2: Int) -> c {
27+
fn add(int: Int, int_2: Int) -> a {
2828
todo
2929
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\nfn wibble(a: a, b: b, c: c) -> d { todo }\n\npub fn main() {\n let x = todo\n let y = todo\n let #(a, b) = something(x, y)\n b\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
fn wibble(a: a, b: b, c: c) -> d { todo }
8+
9+
pub fn main() {
10+
let x = todo
11+
let y = todo
12+
let #(a, b) = something(x, y)
13+
14+
b
15+
}
16+
17+
18+
----- AFTER ACTION
19+
20+
fn wibble(a: a, b: b, c: c) -> d { todo }
21+
22+
pub fn main() {
23+
let x = todo
24+
let y = todo
25+
let #(a, b) = something(x, y)
26+
b
27+
}
28+
29+
fn something(x: a, y: b) -> #(c, d) {
30+
todo
31+
}

compiler-core/src/type_/printer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,15 @@ impl<'a> Printer<'a> {
333333
}
334334
}
335335

336+
pub fn new_without_type_variables(names: &'a Names) -> Self {
337+
Printer {
338+
names,
339+
uid: Default::default(),
340+
printed_type_variables: Default::default(),
341+
printed_type_variable_names: Default::default(),
342+
}
343+
}
344+
336345
pub fn clear_type_variables(&mut self) {
337346
self.printed_type_variable_names.clear();
338347
}

0 commit comments

Comments
 (0)