-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
diagnostics.rs
980 lines (930 loc) · 28.7 KB
/
diagnostics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//! Contains type checking errors, warnings and related structures
#![allow(clippy::upper_case_acronyms)]
use crate::{
context::{environment::Label, information::InformationChain},
diagnostics,
types::{printing::print_type_with_type_arguments, GenericChain, GenericChainLink},
};
use source_map::{SourceId, SpanWithSource};
use std::{
fmt::{self, Debug, Display},
iter,
path::PathBuf,
};
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize), serde(rename_all = "lowercase"))]
#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
pub enum DiagnosticKind {
Error,
Warning,
Info,
}
/// Contains information
#[derive(Debug)]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize), serde(untagged))]
#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
pub enum Diagnostic {
/// Does not have positional information
Global {
reason: String,
kind: DiagnosticKind,
},
Position {
reason: String,
position: SpanWithSource,
kind: DiagnosticKind,
},
PositionWithAdditionalLabels {
reason: String,
position: SpanWithSource,
labels: Vec<(String, Option<SpanWithSource>)>,
kind: DiagnosticKind,
},
}
/// Temporary dead zone. Between the variable identifier being hoisted and the value being assigned
pub struct TDZ {
pub variable_name: String,
pub position: SpanWithSource,
}
pub struct NotInLoopOrCouldNotFindLabel {
pub label: Label,
pub position: SpanWithSource,
}
impl Diagnostic {
pub fn sources(&self) -> impl Iterator<Item = SourceId> + '_ {
use either::{Left, Right};
match self {
Diagnostic::Global { .. } => Left(Left(iter::empty())),
Diagnostic::Position { position: span, .. } => Left(Right(iter::once(span.source))),
Diagnostic::PositionWithAdditionalLabels { position: pos, labels, .. } => {
Right(iter::once(pos.source).chain(
labels.iter().filter_map(|(_, span)| span.as_ref().map(|span| span.source)),
))
}
}
}
#[must_use]
pub fn reason(&self) -> &str {
match self {
Diagnostic::Global { reason, .. }
| Diagnostic::Position { reason, .. }
| Diagnostic::PositionWithAdditionalLabels { reason, .. } => reason,
}
}
#[must_use]
pub fn reason_and_position(self) -> (String, Option<SpanWithSource>) {
match self {
Diagnostic::Global { reason, .. } => (reason, None),
Diagnostic::Position { reason, position, .. }
| Diagnostic::PositionWithAdditionalLabels { reason, position, .. } => (reason, Some(position)),
}
}
#[must_use]
pub fn kind(&self) -> DiagnosticKind {
match self {
Diagnostic::Global { kind, .. }
| Diagnostic::Position { kind, .. }
| Diagnostic::PositionWithAdditionalLabels { kind, .. } => *kind,
}
}
}
/// TODO this is one variant, others should pipe strait to stdout or put it on a channel etc
#[derive(Default)]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize), serde(transparent))]
#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
pub struct DiagnosticsContainer {
diagnostics: Vec<Diagnostic>,
// Quick way to check whether a error was added
#[cfg_attr(feature = "serde-serialize", serde(skip_serializing))]
has_error: bool,
}
// TODO the add methods are the same...
impl DiagnosticsContainer {
#[must_use]
pub fn new() -> Self {
Self { diagnostics: Default::default(), has_error: false }
}
pub fn add_error<T: Into<Diagnostic>>(&mut self, error: T) {
self.has_error = true;
self.diagnostics.push(error.into());
}
pub fn add_warning<T: Into<Diagnostic>>(&mut self, warning: T) {
self.diagnostics.push(warning.into());
}
pub fn add_info<T: Into<Diagnostic>>(&mut self, info: T) {
self.diagnostics.push(info.into());
}
#[must_use]
pub fn has_error(&self) -> bool {
self.has_error
}
pub fn sources(&self) -> impl Iterator<Item = SourceId> + '_ {
self.diagnostics.iter().flat_map(diagnostics::Diagnostic::sources)
}
#[doc(hidden)]
#[must_use]
pub fn get_diagnostics(self) -> Vec<Diagnostic> {
self.diagnostics
}
pub fn into_result(self) -> Result<Self, Self> {
if self.has_error {
Err(self)
} else {
Ok(self)
}
}
}
impl IntoIterator for DiagnosticsContainer {
type Item = Diagnostic;
type IntoIter = std::vec::IntoIter<Diagnostic>;
fn into_iter(self) -> Self::IntoIter {
self.diagnostics.into_iter()
}
}
pub(super) use defined_errors_and_warnings::*;
use crate::types::{printing::print_type, TypeId, TypeStore};
/// TODO could be more things, for instance a property missing etc
pub enum TypeStringRepresentation {
Type(String),
}
pub enum PropertyRepresentation {
Type(String),
StringKey(String),
}
impl TypeStringRepresentation {
#[must_use]
pub fn from_type_id(
id: TypeId,
ctx: &impl InformationChain,
types: &TypeStore,
debug_mode: bool,
) -> Self {
let value = print_type(id, types, ctx, debug_mode);
Self::Type(value)
}
#[must_use]
pub fn from_type_id_with_generics(
id: TypeId,
type_arguments: GenericChain,
ctx: &impl InformationChain,
types: &TypeStore,
debug_mode: bool,
) -> Self {
let value = print_type_with_type_arguments(id, type_arguments, types, ctx, debug_mode);
Self::Type(value)
}
/// TODO working it out
pub(crate) fn from_property_constraint(
property_constraint: crate::context::Logical<crate::PropertyValue>,
generics: GenericChain,
ctx: &impl InformationChain,
types: &TypeStore,
debug_mode: bool,
) -> TypeStringRepresentation {
match property_constraint {
crate::context::Logical::Pure(constraint) => match constraint {
crate::PropertyValue::Value(v) => {
let value = print_type_with_type_arguments(v, generics, types, ctx, debug_mode);
Self::Type(value)
}
crate::PropertyValue::Getter(_) => todo!(),
crate::PropertyValue::Setter(_) => todo!(),
crate::PropertyValue::Deleted => todo!(),
crate::PropertyValue::Dependent { .. } => todo!(),
},
crate::context::Logical::Or { .. } => {
todo!()
// let left = Self::from_property_constraint(*left, None, ctx, types, debug_mode);
// let right = Self::from_property_constraint(*right, None, ctx, types, debug_mode);
// #[allow(irrefutable_let_patterns)]
// if let (TypeStringRepresentation::Type(mut l), TypeStringRepresentation::Type(r)) =
// (left, right)
// {
// l.push_str(&r);
// TypeStringRepresentation::Type(l)
// } else {
// unreachable!()
// }
}
crate::context::Logical::Implies { on, antecedent } => {
if generics.is_some() {
todo!("chaining")
}
Self::from_property_constraint(
*on,
Some(GenericChainLink::Link { parent: None, value: &antecedent }),
ctx,
types,
debug_mode,
)
}
}
}
}
impl Display for TypeStringRepresentation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TypeStringRepresentation::Type(ty) => f.write_str(ty),
}
}
}
pub(crate) struct NoEnvironmentSpecified;
impl From<NoEnvironmentSpecified> for Diagnostic {
fn from(_error: NoEnvironmentSpecified) -> Self {
Diagnostic::Global { reason: "No environment".to_owned(), kind: DiagnosticKind::Error }
}
}
// Contains known internal errors and warnings
// Contained here in a module to separate user facing
mod defined_errors_and_warnings {
use crate::{
context::AssignmentError,
features::{modules::CouldNotOpenFile, operations::MathematicalAndBitwise},
types::calling::FunctionCallingError,
};
use source_map::SpanWithSource;
use crate::Diagnostic;
use super::{
NotInLoopOrCouldNotFindLabel, PropertyRepresentation, TypeStringRepresentation, TDZ,
};
/// Reasons for errors, intermediate type for generating [Diagnostic]s
/// e.g. cannot Call, cannot equate, duplicate key etc
#[allow(unused)]
pub(crate) enum TypeCheckError<'a> {
FunctionCallingError(FunctionCallingError),
PropertyDoesNotExist {
on: TypeStringRepresentation,
property: PropertyRepresentation,
site: SpanWithSource,
},
NotInLoopOrCouldNotFindLabel(NotInLoopOrCouldNotFindLabel),
RestParameterAnnotationShouldBeArrayType(SpanWithSource),
CouldNotFindVariable {
variable: &'a str,
possibles: Vec<&'a str>,
position: SpanWithSource,
},
CouldNotFindType(&'a str, SpanWithSource),
TypeHasNoGenericParameters(String, SpanWithSource),
AssignmentError(AssignmentError),
InvalidComparison(TypeStringRepresentation, TypeStringRepresentation),
InvalidAddition(TypeStringRepresentation, TypeStringRepresentation),
InvalidUnaryOperation(crate::features::operations::PureUnary, TypeStringRepresentation),
ReturnedTypeDoesNotMatch {
expected_return_type: TypeStringRepresentation,
returned_type: TypeStringRepresentation,
/// Can be `None` if it is inferred parameters
annotation_position: Option<SpanWithSource>,
returned_position: SpanWithSource,
},
// TODO are these the same errors?
TypeIsNotIndexable(TypeStringRepresentation),
TypeIsNotIterable(TypeStringRepresentation),
// This could be a syntax error but that is difficult to type...
NonTopLevelExport(SpanWithSource),
FieldNotExported {
file: &'a str,
importing: &'a str,
position: SpanWithSource,
},
InvalidJSXAttribute {
attribute_name: String,
attribute_type: TypeStringRepresentation,
value_type: TypeStringRepresentation,
// TODO
attribute_type_site: (),
value_site: SpanWithSource,
},
InvalidJSXInterpolatedValue {
interpolation_site: SpanWithSource,
expected: TypeStringRepresentation,
found: TypeStringRepresentation,
},
/// for the `satisfies` keyword
NotSatisfied {
at: SpanWithSource,
expected: TypeStringRepresentation,
found: TypeStringRepresentation,
},
// catch type is not compatible with thrown type
CatchTypeDoesNotMatch {
at: SpanWithSource,
expected: TypeStringRepresentation,
found: TypeStringRepresentation,
},
Unsupported {
thing: &'static str,
at: SpanWithSource,
},
ReDeclaredVariable {
name: &'a str,
position: SpanWithSource,
},
/// TODO temp, needs more info
FunctionDoesNotMeetConstraint {
function_constraint: TypeStringRepresentation,
function_type: TypeStringRepresentation,
position: SpanWithSource,
},
StatementsNotRun {
between: SpanWithSource,
},
CannotRedeclareVariable {
name: String,
position: SpanWithSource,
},
// TODO parameter position
GenericArgumentDoesNotMeetRestriction {
parameter_restriction: TypeStringRepresentation,
argument: TypeStringRepresentation,
position: SpanWithSource,
},
NotDefinedOperator(&'static str, SpanWithSource),
PropertyNotWriteable(SpanWithSource),
NotTopLevelImport(SpanWithSource),
DoubleDefaultExport(SpanWithSource),
CannotOpenFile {
file: CouldNotOpenFile,
position: Option<SpanWithSource>,
},
VariableNotDefinedInContext {
variable: &'a str,
expected_context: &'a str,
current_context: String,
position: SpanWithSource,
},
TypeNeedsTypeArguments(&'a str, SpanWithSource),
CannotFindType(&'a str, SpanWithSource),
TypeAlreadyDeclared {
name: String,
position: SpanWithSource,
},
TDZ(TDZ),
InvalidMathematicalOrBitwiseOperation {
operator: MathematicalAndBitwise,
lhs: TypeStringRepresentation,
rhs: TypeStringRepresentation,
position: SpanWithSource,
},
InvalidCast {
position: SpanWithSource,
from: TypeStringRepresentation,
to: TypeStringRepresentation,
},
/// TODO Position = Function body position. Could it be better
/// TODO maybe warning?
UnreachableVariableClosedOver(String, SpanWithSource),
IncompatibleOverloadParameter {
parameter_position: SpanWithSource,
overloaded_parameter_position: SpanWithSource,
parameter: TypeStringRepresentation,
overloaded_parameter: TypeStringRepresentation,
},
IncompatibleOverloadReturnType {
base_position: SpanWithSource,
overload_position: SpanWithSource,
base: TypeStringRepresentation,
overload: TypeStringRepresentation,
},
}
impl From<TypeCheckError<'_>> for Diagnostic {
fn from(error: TypeCheckError<'_>) -> Self {
let kind = super::DiagnosticKind::Error;
match error {
TypeCheckError::CouldNotFindVariable { variable, possibles: _, position } => {
Diagnostic::Position {
reason: format!(
"Could not find variable '{variable}' in scope",
// possibles Consider '{:?}'
),
position,
kind,
}
}
TypeCheckError::CouldNotFindType(reference, pos) => Diagnostic::Position {
reason: format!("Could not find type '{reference}'"),
position: pos,
kind,
},
TypeCheckError::PropertyDoesNotExist { property, on, site } => {
Diagnostic::Position {
reason: match property {
PropertyRepresentation::Type(ty) => format!("No property of type {ty} on {on}"),
PropertyRepresentation::StringKey(property) => format!("No property '{property}' on {on}"),
},
position: site,
kind,
}
}
TypeCheckError::FunctionCallingError(error) => match error {
FunctionCallingError::InvalidArgumentType {
parameter_type,
argument_type,
argument_position,
parameter_position,
restriction,
} => {
if let Some((restriction_pos, restriction)) = restriction {
Diagnostic::PositionWithAdditionalLabels {
reason: format!(
"Argument of type {argument_type} is not assignable to parameter of type {restriction}"
),
position: argument_position,
labels: vec![(
format!(
"{parameter_type} was specialised with type {restriction}"
),
Some(restriction_pos),
)],
kind,
}
} else {
Diagnostic::PositionWithAdditionalLabels {
reason: format!(
"Argument of type {argument_type} is not assignable to parameter of type {parameter_type}",
),
position: argument_position,
labels: vec![(
format!("Parameter has type {parameter_type}"),
Some(parameter_position),
)],
kind,
}
}
}
FunctionCallingError::MissingArgument { parameter_position, call_site } => {
Diagnostic::PositionWithAdditionalLabels {
reason: "Missing argument".into(),
position: call_site,
kind,
labels: vec![(
"(non-optional) Parameter declared here".into(),
Some(parameter_position),
)],
}
}
FunctionCallingError::ExcessArguments { count: _, position } => {
Diagnostic::Position {
reason: "Excess argument".into(),
position,
kind,
}
}
FunctionCallingError::NotCallable { calling, call_site } => {
Diagnostic::Position {
reason: format!("Cannot call type {calling}"),
position: call_site,
kind,
}
}
FunctionCallingError::ReferenceRestrictionDoesNotMatch {
reference: _,
requirement: _,
found: _,
} => todo!(),
// Diagnostic::Position {
// reason: format!(
// "Calling function requires {} to be {}, found {}",
// identifier, requirement, found
// ),
// position: call_site,
// kind,
// },
FunctionCallingError::CyclicRecursion(_, call_site) => Diagnostic::Position {
reason: "Encountered recursion".into(),
position: call_site,
kind,
},
FunctionCallingError::NoLogicForIdentifier(name, position) => Diagnostic::Position { reason: format!("no logic for constant function {name}"), kind, position },
FunctionCallingError::NeedsToBeCalledWithNewKeyword(position) => Diagnostic::Position { reason: "class constructor must be called with new".to_owned(), kind, position },
FunctionCallingError::TDZ { error: TDZ { position, variable_name }, call_site } => Diagnostic::PositionWithAdditionalLabels {
reason: format!("Variable '{variable_name}' used before declaration"),
position: call_site.unwrap(),
kind,
labels: vec![(
"Variable referenced here".to_owned(),
Some(position),
)],
},
FunctionCallingError::SetPropertyConstraint { property_type, value_type, assignment_position, call_site } => Diagnostic::PositionWithAdditionalLabels {
reason: "Invalid assignment to parameter".to_owned(),
position: call_site.unwrap(),
kind,
labels: vec![(
format!(
"Type {value_type} does not meet property constraint {property_type}"
),
Some(assignment_position),
)],
},
FunctionCallingError::UnconditionalThrow { value, call_site } => {
Diagnostic::Position {
reason: format!(
"{value} unconditionally thrown in function"
),
position: call_site.unwrap(),
kind,
}
}
FunctionCallingError::MismatchedThis { call_site, expected, found } => Diagnostic::Position {
reason: format!(
"The 'this' context of the function is expected to be {expected}, found {found}"
),
position: call_site,
kind,
},
},
TypeCheckError::AssignmentError(error) => match error {
AssignmentError::DoesNotMeetConstraint {
variable_type,
variable_site,
value_type,
value_site,
} => Diagnostic::PositionWithAdditionalLabels {
reason: format!(
"Type {value_type} is not assignable to type {variable_type}",
),
position: value_site,
labels: vec![(
format!("Variable declared with type {variable_type}"),
Some(variable_site),
)],
kind,
},
AssignmentError::PropertyConstraint {
property_constraint: property_type,
value_type,
assignment_position,
} => Diagnostic::Position {
reason: format!(
"Type {value_type} does not meet property constraint {property_type}"
),
position: assignment_position,
kind,
},
AssignmentError::Constant(position) => Diagnostic::Position {
reason: "Cannot assign to constant".into(),
position,
kind,
},
AssignmentError::VariableNotFound { variable, assignment_position } => {
Diagnostic::Position {
reason: format!("Cannot assign to unknown variable '{variable}'"),
position: assignment_position,
kind,
}
}
AssignmentError::TDZ(TDZ { variable_name, position }) => {
Diagnostic::Position {
reason: format!("Cannot assign to '{variable_name}' before declaration"),
position,
kind,
}
}
},
TypeCheckError::InvalidJSXAttribute {
attribute_name,
attribute_type,
value_type,
attribute_type_site: _variable_site,
value_site,
} => Diagnostic::Position {
reason: format!(
"Type {attribute_name} is not assignable to {value_type} attribute of type {attribute_type}",
),
position: value_site,
kind,
},
TypeCheckError::ReturnedTypeDoesNotMatch {
annotation_position,
returned_position,
expected_return_type,
returned_type,
} => Diagnostic::PositionWithAdditionalLabels {
reason: format!(
"Cannot return {returned_type} because the function is expected to return {expected_return_type}",
),
labels: annotation_position.into_iter().map(|annotation_position| (
format!("Function annotated to return {expected_return_type} here"),
Some(annotation_position),
)).collect(),
position: returned_position,
kind,
},
TypeCheckError::CatchTypeDoesNotMatch {
expected,
found,
at,
} => Diagnostic::Position {
reason: format!(
"Cannot catch type {found} because the try block throws {expected}",
),
position: at,
kind,
},
TypeCheckError::TypeHasNoGenericParameters(name, position) => {
Diagnostic::Position {
reason: format!("Type '{name}' has no generic parameters",),
position,
kind,
}
}
TypeCheckError::InvalidComparison(_, _) => todo!(),
TypeCheckError::InvalidAddition(_, _) => todo!(),
TypeCheckError::InvalidUnaryOperation(_, _) => todo!(),
TypeCheckError::TypeIsNotIndexable(_) => todo!(),
TypeCheckError::TypeIsNotIterable(_) => todo!(),
TypeCheckError::NonTopLevelExport(position) => Diagnostic::Position {
reason: "Cannot export at not top level".to_owned(),
position,
kind,
},
TypeCheckError::FieldNotExported { file, importing, position } => {
Diagnostic::Position {
reason: format!("{importing} not exported from {file}"),
position,
kind,
}
}
TypeCheckError::InvalidJSXInterpolatedValue {
interpolation_site: _,
expected: _,
found: _,
} => todo!(),
TypeCheckError::RestParameterAnnotationShouldBeArrayType(pos) => {
Diagnostic::Position {
reason: "Rest parameter annotation should be array type".to_owned(),
position: pos,
kind,
}
}
TypeCheckError::Unsupported { thing, at } => Diagnostic::Position {
reason: format!("Unsupported: {thing}"),
position: at,
kind,
},
TypeCheckError::ReDeclaredVariable { name, position } => {
Diagnostic::Position {
reason: format!("Cannot declare variable {name}"),
position,
kind,
}
}
TypeCheckError::FunctionDoesNotMeetConstraint {
function_constraint,
function_type,
position,
} => Diagnostic::Position {
reason: format!(
"{function_constraint} constraint on function does not match synthesised form {function_type}",
),
position,
kind,
},
TypeCheckError::StatementsNotRun { between } => Diagnostic::Position {
reason: "Statements are never run".to_owned(),
position: between,
kind,
},
TypeCheckError::NotSatisfied { at, expected, found } => Diagnostic::Position {
reason: format!("Expected {expected}, found {found}"),
position: at,
kind,
},
TypeCheckError::CannotRedeclareVariable { name, position } => {
Diagnostic::Position {
reason: format!("Cannot redeclare variable '{name}'"),
position,
kind,
}
}
TypeCheckError::NotDefinedOperator(op, position) => Diagnostic::Position {
reason: format!("Operator not typed {op}"),
position,
kind,
},
TypeCheckError::PropertyNotWriteable(position) => Diagnostic::Position {
reason: "Property not writeable".into(),
position,
kind,
},
TypeCheckError::GenericArgumentDoesNotMeetRestriction {
argument,
parameter_restriction,
position,
} => Diagnostic::Position {
reason: format!(
"Generic argument {argument} does not match {parameter_restriction}"
),
position,
kind,
},
TypeCheckError::NotTopLevelImport(position) => Diagnostic::Position {
reason: "Import must be in the top of the scope".to_owned(),
position,
kind,
},
TypeCheckError::DoubleDefaultExport(_) => todo!(),
TypeCheckError::CannotOpenFile { file, position } => if let Some(position) = position {
Diagnostic::Position {
reason: "Cannot find file".to_owned(),
position,
kind,
}
} else {
Diagnostic::Global { reason: format!("Cannot find file {}", file.0.display()), kind }
},
TypeCheckError::VariableNotDefinedInContext {
variable,
expected_context,
current_context,
position,
} => Diagnostic::Position {
reason: format!("'{variable}' is only available on the {expected_context}, currently in {current_context}"),
position,
kind,
},
TypeCheckError::TypeNeedsTypeArguments(ty, position) => Diagnostic::Position {
reason: format!("Type {ty} requires type arguments"),
position,
kind,
},
TypeCheckError::CannotFindType(ty, position) => Diagnostic::Position {
reason: format!("Cannot find type {ty}"),
position,
kind,
},
TypeCheckError::TypeAlreadyDeclared { name, position } => Diagnostic::Position {
reason: format!("Type named '{name}' already declared"),
position,
kind,
},
TypeCheckError::TDZ(TDZ { position, variable_name }) => Diagnostic::Position {
reason: format!("Variable '{variable_name}' used before declaration"),
position,
kind,
},
TypeCheckError::InvalidMathematicalOrBitwiseOperation { operator, lhs, rhs, position } => Diagnostic::Position {
// TODO temp
reason: format!("Cannot {lhs} {operator:?} {rhs}"),
position,
kind,
},
TypeCheckError::NotInLoopOrCouldNotFindLabel(_) => todo!(),
TypeCheckError::InvalidCast { position, from, to } => {
Diagnostic::Position {
reason: format!("Cannot cast {from} to {to}"),
position,
kind,
}
}
TypeCheckError::UnreachableVariableClosedOver(name, function_position) => {
Diagnostic::Position {
reason: format!("Function contains unreachable closed over variable '{name}'"),
position: function_position,
kind,
}
}
TypeCheckError::IncompatibleOverloadParameter { parameter_position, overloaded_parameter_position, parameter, overloaded_parameter } => Diagnostic::PositionWithAdditionalLabels {
reason: format!(
"Overload with parameter of {overloaded_parameter} does not meet base parameter {parameter}",
),
labels: vec![(
format!("Function has base type {parameter} here"),
Some(parameter_position),
)],
position: overloaded_parameter_position,
kind,
},
TypeCheckError::IncompatibleOverloadReturnType { base_position, overload_position, base, overload } => Diagnostic::PositionWithAdditionalLabels {
reason: format!(
"Cannot return {overload} in overload because base function is expected to return {base}",
),
labels: vec![(
format!("Function annotated to return {base} here"),
Some(base_position),
)],
position: overload_position,
kind,
},
}
}
}
pub enum TypeCheckWarning {
AwaitUsedOnNonPromise(SpanWithSource),
/// TODO could be an error at some point
DeadBranch {
expression_span: SpanWithSource,
expression_value: bool,
},
IgnoringAsExpression(SpanWithSource),
Unimplemented {
thing: &'static str,
at: SpanWithSource,
},
UselessExpression {
expression_span: SpanWithSource,
// TODO other branch information
},
MergingInterfaceInSameContext {
position: SpanWithSource,
},
TypesDoNotIntersect {
left: TypeStringRepresentation,
right: TypeStringRepresentation,
position: SpanWithSource,
},
InvalidOrUnimplementedDefinitionFileItem(SpanWithSource),
Unreachable(SpanWithSource),
}
impl From<TypeCheckWarning> for Diagnostic {
fn from(warning: TypeCheckWarning) -> Self {
let kind = super::DiagnosticKind::Warning;
match warning {
TypeCheckWarning::AwaitUsedOnNonPromise(position) => Diagnostic::Position {
reason: "Unnecessary await expression / type is not promise".to_owned(),
position,
kind,
},
TypeCheckWarning::DeadBranch { expression_span, expression_value } => {
Diagnostic::Position {
reason: format!("Expression is always {expression_value:?}"),
position: expression_span,
kind,
}
}
TypeCheckWarning::IgnoringAsExpression(position) => Diagnostic::Position {
reason: "'as' expressions are ignore by the checker".to_owned(),
position,
kind,
},
TypeCheckWarning::Unimplemented { thing, at } => Diagnostic::Position {
reason: format!("Unsupported: {thing}"),
position: at,
kind,
},
TypeCheckWarning::UselessExpression { expression_span } => Diagnostic::Position {
reason: "Expression is always true".to_owned(),
position: expression_span,
kind,
},
TypeCheckWarning::MergingInterfaceInSameContext { position } => {
Diagnostic::Position {
reason: "Merging interfaces in the same context".to_owned(),
position,
kind,
}
}
TypeCheckWarning::TypesDoNotIntersect { left, right, position } => {
Diagnostic::Position {
reason: format!("No intersection between types {left} and {right}"),
position,
kind,
}
}
TypeCheckWarning::InvalidOrUnimplementedDefinitionFileItem(position) => {
Diagnostic::Position {
reason: "Invalid (or unimplemented) item in definition file skipped"
.to_owned(),
position,
kind,
}
}
TypeCheckWarning::Unreachable(position) => Diagnostic::Position {
reason: "Unreachable statement".to_owned(),
position,
kind,
},
}
}
}
}
#[derive(Debug)]
pub struct CannotFindTypeError<'a>(pub &'a str);
#[derive(Debug)]
pub struct TypeIsNotIndexable<'a>(pub &'a TypeId);
pub(crate) struct TypeDefinitionModuleNotFound(pub PathBuf);
impl From<TypeDefinitionModuleNotFound> for Diagnostic {
fn from(val: TypeDefinitionModuleNotFound) -> Self {
Diagnostic::Global {
reason: format!(
"Could not find type definition module at '{}'",
val.0.as_path().display()
),
kind: DiagnosticKind::Error,
}
}
}
pub(crate) struct EntryPointNotFound(pub PathBuf);
impl From<EntryPointNotFound> for Diagnostic {
fn from(val: EntryPointNotFound) -> Self {
Diagnostic::Global {
reason: format!("Could not entry point module at '{}'", val.0.as_path().display()),
kind: DiagnosticKind::Error,
}
}
}
#[derive(Debug)]
pub struct CannotRedeclareVariable<'a> {
pub name: &'a str,
}