Skip to content

Commit f8e45cb

Browse files
committed
More fixes
1 parent 95f4c39 commit f8e45cb

19 files changed

+212
-100
lines changed

compiler-core/src/ast/constant.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use crate::analyse::Inferred;
23
use crate::type_::{FieldMap, HasType};
34

45
pub type TypedConstant = Constant<Arc<Type>, EcoString>;
@@ -42,7 +43,7 @@ pub enum Constant<T, RecordTag> {
4243
arguments: Vec<CallArg<Self>>,
4344
tag: RecordTag,
4445
type_: T,
45-
field_map: Option<FieldMap>,
46+
field_map: Inferred<FieldMap>,
4647
record_constructor: Option<Box<ValueConstructor>>,
4748
},
4849

@@ -54,7 +55,7 @@ pub enum Constant<T, RecordTag> {
5455
arguments: Vec<ConstantRecordUpdateArg<Self>>,
5556
tag: RecordTag,
5657
type_: T,
57-
field_map: Option<FieldMap>,
58+
field_map: Inferred<FieldMap>,
5859
},
5960

6061
BitArray {

compiler-core/src/ast_folder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ pub trait UntypedConstantFolder {
10781078
arguments,
10791079
tag: (),
10801080
type_: (),
1081-
field_map: None,
1081+
field_map: Inferred::Unknown,
10821082
record_constructor: None,
10831083
}
10841084
}
@@ -1099,7 +1099,7 @@ pub trait UntypedConstantFolder {
10991099
arguments,
11001100
tag: (),
11011101
type_: (),
1102-
field_map: None,
1102+
field_map: Inferred::Unknown,
11031103
}
11041104
}
11051105

compiler-core/src/error.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::strings::{to_snake_case, to_upper_camel_case};
1010
use crate::type_::collapse_links;
1111
use crate::type_::error::{
1212
IncorrectArityContext, InvalidImportKind, MissingAnnotation, ModuleValueUsageContext, Named,
13-
UnknownField, UnknownTypeHint, UnsafeRecordUpdateReason,
13+
RecordUpdateInvalidReason, UnknownField, UnknownTypeHint, UnsafeRecordUpdateReason,
1414
};
1515
use crate::type_::printer::{Names, Printer};
1616
use crate::type_::{FieldAccessUsage, error::PatternMatchKind};
@@ -3096,21 +3096,43 @@ UTF-codepoint pattern matching."
30963096
}),
30973097
}
30983098
}
3099-
TypeError::RecordUpdateInvalidConstructor { location } => Diagnostic {
3100-
title: "Invalid record constructor".into(),
3101-
text: "Only record constructors can be used with the update syntax.".into(),
3102-
hint: None,
3103-
level: Level::Error,
3104-
location: Some(Location {
3105-
label: Label {
3106-
text: Some("This is not a record constructor".into()),
3107-
span: *location,
3108-
},
3109-
path: path.clone(),
3110-
src: src.clone(),
3111-
extra_labels: vec![],
3112-
}),
3113-
},
3099+
TypeError::RecordUpdateInvalidConstructor { location, reason } => {
3100+
let (title, text, label_text) = match reason {
3101+
RecordUpdateInvalidReason::NoFields => (
3102+
"Invalid record update".into(),
3103+
"This constructor has no fields to update.".into(),
3104+
"This constructor has no fields".into(),
3105+
),
3106+
RecordUpdateInvalidReason::UnlabelledFields => (
3107+
"Invalid record update".into(),
3108+
"Only constructors with labelled fields can be used with the update syntax.".into(),
3109+
"This constructor has no labelled fields".into(),
3110+
),
3111+
RecordUpdateInvalidReason::WrongVariant { expected, given } => (
3112+
"Type mismatch".into(),
3113+
wrap(&format!(
3114+
"The record being spread is a `{}` but you are trying to construct a `{}`.",
3115+
given, expected
3116+
)),
3117+
format!("This is a `{}`, not a `{}`", given, expected),
3118+
),
3119+
};
3120+
Diagnostic {
3121+
title,
3122+
text,
3123+
hint: None,
3124+
level: Level::Error,
3125+
location: Some(Location {
3126+
label: Label {
3127+
text: Some(label_text),
3128+
span: *location,
3129+
},
3130+
path: path.clone(),
3131+
src: src.clone(),
3132+
extra_labels: vec![],
3133+
}),
3134+
}
3135+
}
31143136

31153137
TypeError::UnexpectedTypeHole { location } => Diagnostic {
31163138
title: "Unexpected type hole".into(),

compiler-core/src/metadata/module_decoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use itertools::Itertools;
66

77
use crate::{
88
Result,
9+
analyse::Inferred,
910
ast::{
1011
BitArrayOption, BitArraySegment, CallArg, Constant, Publicity, SrcSpan, TypedConstant,
1112
TypedConstantBitArraySegment, TypedConstantBitArraySegmentOption,
@@ -439,7 +440,7 @@ impl ModuleDecoder {
439440
arguments,
440441
tag,
441442
type_,
442-
field_map: None,
443+
field_map: Inferred::Unknown,
443444
record_constructor: None,
444445
})
445446
}

compiler-core/src/metadata/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use type_::{AccessorsMap, FieldMap, RecordAccessor};
44

55
use super::*;
66
use crate::{
7+
analyse::Inferred,
78
ast::{
89
BitArrayOption, BitArraySegment, CallArg, Constant, Publicity, SrcSpan, TypedConstant,
910
TypedConstantBitArraySegmentOption,
@@ -1166,7 +1167,7 @@ fn constant_record() {
11661167
],
11671168
tag: "thetag".into(),
11681169
type_: type_::int(),
1169-
field_map: None,
1170+
field_map: Inferred::Unknown,
11701171
record_constructor: None,
11711172
});
11721173

compiler-core/src/parse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,7 +3365,7 @@ where
33653365
arguments: update_arguments,
33663366
tag: (),
33673367
type_: (),
3368-
field_map: None,
3368+
field_map: Inferred::Unknown,
33693369
}))
33703370
} else {
33713371
// No spread - parse as regular Record construction
@@ -3394,7 +3394,7 @@ where
33943394
arguments,
33953395
tag: (),
33963396
type_: (),
3397-
field_map: None,
3397+
field_map: Inferred::Unknown,
33983398
record_constructor: None,
33993399
}))
34003400
}
@@ -3406,7 +3406,7 @@ where
34063406
arguments: vec![],
34073407
tag: (),
34083408
type_: (),
3409-
field_map: None,
3409+
field_map: Inferred::Unknown,
34103410
record_constructor: None,
34113411
})),
34123412
}

compiler-core/src/parse/snapshots/gleam_core__parse__tests__const_record_spread_all_fields.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ Parsed {
221221
],
222222
tag: (),
223223
type_: (),
224-
field_map: None,
224+
field_map: Unknown,
225225
record_constructor: None,
226226
},
227227
type_: (),
@@ -302,7 +302,7 @@ Parsed {
302302
],
303303
tag: (),
304304
type_: (),
305-
field_map: None,
305+
field_map: Unknown,
306306
},
307307
type_: (),
308308
deprecation: NotDeprecated,

compiler-core/src/parse/snapshots/gleam_core__parse__tests__const_record_spread_basic.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Parsed {
173173
],
174174
tag: (),
175175
type_: (),
176-
field_map: None,
176+
field_map: Unknown,
177177
record_constructor: None,
178178
},
179179
type_: (),
@@ -239,7 +239,7 @@ Parsed {
239239
],
240240
tag: (),
241241
type_: (),
242-
field_map: None,
242+
field_map: Unknown,
243243
},
244244
type_: (),
245245
deprecation: NotDeprecated,

compiler-core/src/parse/snapshots/gleam_core__parse__tests__const_record_spread_only.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Parsed {
173173
],
174174
tag: (),
175175
type_: (),
176-
field_map: None,
176+
field_map: Unknown,
177177
record_constructor: None,
178178
},
179179
type_: (),
@@ -224,7 +224,7 @@ Parsed {
224224
arguments: [],
225225
tag: (),
226226
type_: (),
227-
field_map: None,
227+
field_map: Unknown,
228228
},
229229
type_: (),
230230
deprecation: NotDeprecated,

compiler-core/src/parse/snapshots/gleam_core__parse__tests__const_record_spread_with_module.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Parsed {
7777
],
7878
tag: (),
7979
type_: (),
80-
field_map: None,
80+
field_map: Unknown,
8181
},
8282
type_: (),
8383
deprecation: NotDeprecated,

0 commit comments

Comments
 (0)