lisette-semantics 0.1.6

Little language inspired by Rust that compiles to Go
Documentation
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
use syntax::ast::{BinaryOperator, Expression, Literal, Span, UnaryOperator};
use syntax::program::Definition;
use syntax::types::{Type, substitute};

use BinaryOperator::*;
use UnaryOperator::*;

use super::super::Checker;

impl Checker<'_, '_> {
    pub(super) fn infer_unary(
        &mut self,
        operator: UnaryOperator,
        operand: Box<Expression>,
        expected_ty: &Type,
        span: Span,
    ) -> Expression {
        // For negation with numeric expected type, propagate it to the operand
        // so literals can adapt (e.g., `let x: int8 = -1` works)
        let operand_expected_ty = if operator == Negative && expected_ty.resolve().is_numeric() {
            expected_ty.clone()
        } else {
            self.new_type_var()
        };

        if operator == Negative {
            self.scopes.increment_negation_depth();
        }

        let new_expression =
            self.with_value_context(|s| s.infer_expression(*operand, &operand_expected_ty));

        if operator == Negative {
            self.scopes.decrement_negation_depth();
        }
        self.check_not_temp_producing(&new_expression);
        let operand_span = new_expression.get_span();

        let expression_ty = match operator {
            Negative => {
                let resolved = operand_expected_ty.resolve();
                if resolved.is_numeric() || resolved.underlying_numeric_type().is_some() {
                    let is_literal = is_numeric_literal(&new_expression);
                    if resolved.is_unsigned_int() && !is_literal {
                        let type_name = resolved.get_name().unwrap_or_default();
                        self.sink
                            .push(diagnostics::infer::cannot_negate_unsigned(type_name, span));
                    }
                    self.check_negative_literal_overflow(&new_expression, &resolved, span);
                    operand_expected_ty.clone()
                } else {
                    self.sink
                        .push(diagnostics::infer::not_numeric(&resolved, operand_span));
                    operand_expected_ty.clone()
                }
            }
            Not => {
                let bool_ty = self.type_bool();
                self.unify(&bool_ty, &operand_expected_ty, &span);
                bool_ty
            }
            Deref => {
                let inner_ty = self.new_type_var();
                let ref_ty = self.type_reference(inner_ty.clone());
                self.unify(&ref_ty, &operand_expected_ty, &span);
                inner_ty
            }
        };

        self.unify(expected_ty, &expression_ty, &span);

        Expression::Unary {
            operator,
            expression: new_expression.into(),
            ty: expression_ty,
            span,
        }
    }

    pub(super) fn infer_binary(
        &mut self,
        operator: BinaryOperator,
        left_operand: Box<Expression>,
        right_operand: Box<Expression>,
        expected_ty: &Type,
        span: Span,
    ) -> Expression {
        if matches!(*left_operand, Expression::Binary { .. }) {
            let mut stack = vec![(operator, right_operand, span)];
            let mut current = *left_operand;
            while let Expression::Binary {
                operator: op,
                left,
                right,
                span: s,
                ..
            } = current
            {
                stack.push((op, right, s));
                current = *left;
            }
            let mut left_ty = self.new_type_var();
            let mut left_inferred = self.infer_expression(current, &left_ty);
            while let Some((op, right, s)) = stack.pop() {
                let result_ty = if stack.is_empty() {
                    expected_ty.clone()
                } else {
                    self.new_type_var()
                };
                let (inferred, ty) =
                    self.infer_binary_with_left(op, left_inferred, left_ty, right, &result_ty, s);
                left_inferred = inferred;
                left_ty = ty;
            }
            return left_inferred;
        }

        self.infer_binary_impl(operator, left_operand, right_operand, expected_ty, span)
    }

    /// Infer a binary expression where the left operand is already inferred.
    /// Returns the inferred expression and its result type.
    fn infer_binary_with_left(
        &mut self,
        operator: BinaryOperator,
        left_inferred: Expression,
        left_ty: Type,
        right_operand: Box<Expression>,
        expected_ty: &Type,
        span: Span,
    ) -> (Expression, Type) {
        if matches!(operator, Division | Remainder) {
            let is_zero = match right_operand.unwrap_parens() {
                Expression::Literal {
                    literal: Literal::Integer { value: 0, .. },
                    ..
                } => true,
                Expression::Literal {
                    literal: Literal::Float { value, .. },
                    ..
                } => *value == 0.0,
                _ => false,
            };
            if is_zero {
                self.sink.push(diagnostics::infer::division_by_zero(span));
            }
        }

        let left_operand_ty = left_ty;
        let right_operand_ty = self.new_type_var();

        let right_literal_kind = numeric_literal_kind(&right_operand);
        let is_right_literal = !matches!(right_literal_kind, NumericLiteralKind::None);

        let new_right_operand = self.with_value_context(|s| {
            if is_right_literal {
                let left_resolved = left_operand_ty.resolve();
                if literal_can_adapt_to(&right_literal_kind, &left_resolved) {
                    let _ = s.try_unify(&right_operand_ty, &left_resolved, &span);
                }
            }
            s.infer_expression(*right_operand, &right_operand_ty)
        });

        self.check_not_temp_producing(&left_inferred);
        self.check_not_temp_producing(&new_right_operand);

        if matches!(operator, And | Or)
            && let Some(span) = Checker::find_propagate(&new_right_operand)
        {
            self.sink
                .push(diagnostics::infer::propagate_in_condition(span));
        }

        let left_span = left_inferred.get_span();
        let right_span = new_right_operand.get_span();

        let expression_ty = self.resolve_binary_type(
            &operator,
            &left_operand_ty,
            &right_operand_ty,
            &left_span,
            &right_span,
            span,
        );

        self.unify(expected_ty, &expression_ty, &span);

        let result = Expression::Binary {
            operator,
            left: Box::new(left_inferred),
            right: Box::new(new_right_operand),
            ty: expression_ty.clone(),
            span,
        };
        (result, expression_ty)
    }

    fn infer_binary_impl(
        &mut self,
        operator: BinaryOperator,
        left_operand: Box<Expression>,
        right_operand: Box<Expression>,
        expected_ty: &Type,
        span: Span,
    ) -> Expression {
        if matches!(operator, Division | Remainder) {
            let is_zero = match right_operand.unwrap_parens() {
                Expression::Literal {
                    literal: Literal::Integer { value: 0, .. },
                    ..
                } => true,
                Expression::Literal {
                    literal: Literal::Float { value, .. },
                    ..
                } => *value == 0.0,
                _ => false,
            };
            if is_zero {
                self.sink.push(diagnostics::infer::division_by_zero(span));
            }
        }

        let left_operand_ty = self.new_type_var();
        let right_operand_ty = self.new_type_var();

        // Check for numeric literals before inference so we can propagate
        // type information from the non-literal operand to the literal.
        // This enables coercion like `b == 0` where b: float64 → 0 becomes float64.
        //
        // Integer literals adapt when the target `is_numeric()` (int, float, etc.).
        // Float literals adapt only when the target `is_float()` (float32, float64).
        let left_literal_kind = numeric_literal_kind(&left_operand);
        let right_literal_kind = numeric_literal_kind(&right_operand);
        let is_left_literal = !matches!(left_literal_kind, NumericLiteralKind::None);
        let is_right_literal = !matches!(right_literal_kind, NumericLiteralKind::None);

        let (new_left_operand, new_right_operand) = self.with_value_context(|s| {
            if is_left_literal && !is_right_literal {
                // Infer the non-literal (right) first so its resolved type
                // can guide the literal's type adaptation.
                let right = s.infer_expression(*right_operand, &right_operand_ty);
                let right_resolved = right_operand_ty.resolve();
                if literal_can_adapt_to(&left_literal_kind, &right_resolved) {
                    let _ = s.try_unify(&left_operand_ty, &right_resolved, &span);
                }
                let left = s.infer_expression(*left_operand, &left_operand_ty);
                (left, right)
            } else {
                let left = s.infer_expression(*left_operand, &left_operand_ty);
                if is_right_literal {
                    let left_resolved = left_operand_ty.resolve();
                    if literal_can_adapt_to(&right_literal_kind, &left_resolved) {
                        let _ = s.try_unify(&right_operand_ty, &left_resolved, &span);
                    }
                }
                let right = s.infer_expression(*right_operand, &right_operand_ty);
                (left, right)
            }
        });

        self.check_not_temp_producing(&new_left_operand);
        self.check_not_temp_producing(&new_right_operand);

        if matches!(operator, And | Or)
            && let Some(span) = Checker::find_propagate(&new_right_operand)
        {
            self.sink
                .push(diagnostics::infer::propagate_in_condition(span));
        }

        let left_span = new_left_operand.get_span();
        let right_span = new_right_operand.get_span();

        let expression_ty = self.resolve_binary_type(
            &operator,
            &left_operand_ty,
            &right_operand_ty,
            &left_span,
            &right_span,
            span,
        );

        self.unify(expected_ty, &expression_ty, &span);

        Expression::Binary {
            operator,
            left: new_left_operand.into(),
            right: new_right_operand.into(),
            ty: expression_ty,
            span,
        }
    }

    /// Resolve the result type of a binary operation given already-inferred operand types.
    fn resolve_binary_type(
        &mut self,
        operator: &BinaryOperator,
        left_operand_ty: &Type,
        right_operand_ty: &Type,
        left_span: &Span,
        right_span: &Span,
        span: Span,
    ) -> Type {
        match operator {
            Equal | NotEqual => {
                let resolved_left_operand = left_operand_ty.resolve();
                let resolved_right_operand = right_operand_ty.resolve();

                let same_aliased_numeric = resolved_left_operand == resolved_right_operand
                    && resolved_left_operand.is_aliased_numeric_type();

                let different_but_compatible = resolved_left_operand != resolved_right_operand
                    && resolved_left_operand.is_numeric_compatible_with(&resolved_right_operand);

                if !same_aliased_numeric && !different_but_compatible {
                    self.unify_binary_operands(operator, left_operand_ty, right_operand_ty, &span);
                }
                self.ensure_comparable(left_operand_ty, left_span);
                self.type_bool()
            }

            And | Or => {
                let bool_ty = self.type_bool();
                self.unify(left_operand_ty, &bool_ty, &span);
                self.unify(right_operand_ty, &bool_ty, &span);
                bool_ty
            }

            LessThan | LessThanOrEqual | GreaterThan | GreaterThanOrEqual => {
                let resolved_left_operand = left_operand_ty.resolve();
                let resolved_right_operand = right_operand_ty.resolve();

                let same_aliased_numeric = resolved_left_operand == resolved_right_operand
                    && resolved_left_operand.is_aliased_numeric_type();

                let different_but_compatible = resolved_left_operand != resolved_right_operand
                    && resolved_left_operand.is_numeric_compatible_with(&resolved_right_operand);

                if same_aliased_numeric || different_but_compatible {
                    self.type_bool()
                } else {
                    self.ensure_orderable(left_operand_ty, left_span);
                    self.ensure_orderable(right_operand_ty, right_span);
                    self.unify_binary_operands(operator, left_operand_ty, right_operand_ty, &span);
                    self.type_bool()
                }
            }

            Addition => {
                let resolved_left_operand = left_operand_ty.resolve();
                let resolved_right_operand = right_operand_ty.resolve();

                if let Some(result_ty) = self.try_operation_with_numeric_alias(
                    operator,
                    &resolved_left_operand,
                    &resolved_right_operand,
                    &span,
                ) {
                    result_ty
                } else {
                    let numeric_ok = if !resolved_left_operand.is_string()
                        && !resolved_right_operand.is_string()
                    {
                        self.ensure_numeric_for_binary(operator, left_operand_ty, left_span)
                            & self.ensure_numeric_for_binary(operator, right_operand_ty, right_span)
                    } else {
                        true
                    };

                    if resolved_left_operand.is_complex() || resolved_right_operand.is_complex() {
                        self.type_complex128()
                    } else {
                        if numeric_ok {
                            self.unify_binary_operands(
                                operator,
                                left_operand_ty,
                                right_operand_ty,
                                &span,
                            );
                        }
                        left_operand_ty.clone()
                    }
                }
            }

            Subtraction | Multiplication | Division | Remainder => {
                let left_resolved = left_operand_ty.resolve();
                let right_resolved = right_operand_ty.resolve();

                if matches!(operator, Remainder)
                    && (left_resolved.is_float() || right_resolved.is_float())
                {
                    self.sink
                        .push(diagnostics::infer::float_modulo_not_supported(span));
                }

                if let Some(result_ty) = self.try_operation_with_numeric_alias(
                    operator,
                    &left_resolved,
                    &right_resolved,
                    &span,
                ) {
                    result_ty
                } else if left_resolved.is_complex() || right_resolved.is_complex() {
                    self.type_complex128()
                } else {
                    let left_ok =
                        self.ensure_numeric_for_binary(operator, left_operand_ty, left_span);
                    let right_ok =
                        self.ensure_numeric_for_binary(operator, right_operand_ty, right_span);
                    if left_ok && right_ok {
                        self.unify_binary_operands(
                            operator,
                            left_operand_ty,
                            right_operand_ty,
                            &span,
                        );
                    }
                    left_operand_ty.clone()
                }
            }

            Pipeline => {
                panic!("Pipeline operator should have been desugared before type inference")
            }
        }
    }

    /// Returns `true` if the type is numeric (or unresolved), `false` if an error was emitted.
    fn ensure_numeric_for_binary(
        &mut self,
        operator: &BinaryOperator,
        ty: &Type,
        span: &Span,
    ) -> bool {
        let resolved_ty = ty.resolve();
        // Type variables (unresolved inference vars) are allowed — they'll be resolved later.
        // But type parameters (generic T without bounds) should be rejected:
        // Go requires `constraints.Ordered` for arithmetic on type params.
        if matches!(resolved_ty, Type::Variable(_) | Type::Error) {
            return true;
        }
        if matches!(resolved_ty, Type::Parameter(_)) {
            self.sink
                .push(diagnostics::infer::not_orderable(&resolved_ty, *span));
            return false;
        }
        if !resolved_ty.is_numeric() {
            self.sink.push(diagnostics::infer::not_numeric_for_binary(
                operator,
                &resolved_ty,
                *span,
            ));
            return false;
        }
        true
    }

    fn ensure_orderable(&mut self, ty: &Type, span: &Span) {
        let resolved_ty = ty.resolve();

        if resolved_ty.is_error() {
            return;
        }

        if !resolved_ty.is_ordered() && !resolved_ty.is_string() && !resolved_ty.is_boolean() {
            self.sink
                .push(diagnostics::infer::not_orderable(&resolved_ty, *span));
        }
    }

    fn ensure_comparable(&mut self, ty: &Type, span: &Span) {
        let resolved = ty.resolve();
        if resolved.is_error() {
            return;
        }
        if let Some(reason) = self.check_not_comparable(&resolved) {
            self.sink
                .push(diagnostics::infer::not_comparable(&resolved, reason, *span));
        }
    }

    fn check_not_comparable(&self, ty: &Type) -> Option<&'static str> {
        if matches!(ty, Type::Function { .. }) {
            return Some("functions");
        }

        if ty.has_name("Slice") {
            return Some("slices");
        }
        if ty.has_name("Map") {
            return Some("maps");
        }

        // Ref and Channel are always comparable (pointer/reference equality) — don't recurse
        if ty.has_name("Ref") || ty.has_name("Channel") {
            return None;
        }

        if matches!(ty, Type::Variable(_)) {
            return None;
        }

        // Type parameters (generic T) — Go requires `comparable` constraint
        if matches!(ty, Type::Parameter(_)) {
            return Some("type parameters (Go requires the `comparable` constraint)");
        }

        // Structs and enums: recurse into fields with generic substitution.
        // For generic types like Option<Color>, the definition has fields with
        // type parameter T — substitute T → Color before checking comparability.
        if let Some(name) = ty.get_qualified_id()
            && let Some(definition) = self.store.get_definition(name)
        {
            let type_args = ty.get_type_params().unwrap_or_default();
            let generics = match &definition {
                Definition::Struct { generics, .. } | Definition::Enum { generics, .. } => {
                    generics.as_slice()
                }
                _ => &[],
            };
            let sub_map = generics
                .iter()
                .map(|g| g.name.clone())
                .zip(type_args.iter().cloned())
                .collect();

            match definition {
                Definition::Struct { fields, .. } => {
                    for f in fields {
                        let field_ty = substitute(&f.ty.resolve(), &sub_map);
                        if self.check_not_comparable(&field_ty).is_some() {
                            return Some("a struct containing non-comparable fields");
                        }
                    }
                }
                Definition::Enum { variants, .. } => {
                    for v in variants {
                        for f in v.fields.iter() {
                            let field_ty = substitute(&f.ty.resolve(), &sub_map);
                            if self.check_not_comparable(&field_ty).is_some() {
                                return Some("an enum containing non-comparable fields");
                            }
                        }
                    }
                }
                _ => {}
            }
        }

        if let Type::Tuple(elems) = ty {
            for e in elems {
                if self.check_not_comparable(&e.resolve()).is_some() {
                    return Some("a tuple containing non-comparable elements");
                }
            }
        }

        None
    }

    fn unify_binary_operands(
        &mut self,
        operator: &BinaryOperator,
        left_operand_ty: &Type,
        right_operand_ty: &Type,
        span: &Span,
    ) {
        if self
            .try_unify(left_operand_ty, right_operand_ty, span)
            .is_err()
        {
            self.sink
                .push(diagnostics::infer::binary_operator_type_mismatch(
                    operator,
                    left_operand_ty,
                    right_operand_ty,
                    *span,
                ));
        }
    }

    fn try_operation_with_numeric_alias(
        &mut self,
        operator: &BinaryOperator,
        left_ty: &Type,
        right_ty: &Type,
        span: &Span,
    ) -> Option<Type> {
        let left_underlying = left_ty.underlying_numeric_type();
        let right_underlying = right_ty.underlying_numeric_type();

        let (left_underlying, right_underlying) = match (left_underlying, right_underlying) {
            (Some(l), Some(r)) => (l, r),
            _ => return None,
        };

        let left_family = left_underlying.numeric_family()?;
        let right_family = right_underlying.numeric_family()?;

        if left_family != right_family {
            return None;
        }

        let left_is_aliased = left_ty.is_aliased_numeric_type();
        let right_is_aliased = right_ty.is_aliased_numeric_type();

        match (left_is_aliased, right_is_aliased, operator) {
            (true, true, _) if left_ty == right_ty => {
                if matches!(operator, Division) {
                    // T / T → U (ratio yields underlying type)
                    Some(left_underlying)
                } else {
                    Some(left_ty.clone())
                }
            }

            (true, false, _) => Some(left_ty.clone()),

            (false, true, Division | Remainder) => {
                self.sink.push(diagnostics::infer::invalid_division_order(
                    operator, left_ty, right_ty, *span,
                ));
                None
            }
            (false, true, _) => Some(right_ty.clone()),

            (false, false, _) => None,

            (true, true, _) => {
                self.sink
                    .push(diagnostics::infer::incompatible_named_numeric_types(
                        &left_underlying,
                        *span,
                    ));
                None
            }
        }
    }

    pub(super) fn infer_range(
        &mut self,
        start: Option<Box<Expression>>,
        end: Option<Box<Expression>>,
        inclusive: bool,
        span: Span,
        expected_ty: &Type,
    ) -> Expression {
        let element_ty = self.new_type_var();

        let (new_start, new_end) = self.with_value_context(|s| {
            let start =
                start.map(|expression| Box::new(s.infer_expression(*expression, &element_ty)));
            let end = end.map(|expression| Box::new(s.infer_expression(*expression, &element_ty)));
            (start, end)
        });

        if let Some(s) = &new_start {
            self.check_not_temp_producing(s);
        }
        if let Some(e) = &new_end {
            self.check_not_temp_producing(e);
        }

        let range_ty = match (&new_start, &new_end, inclusive) {
            (Some(_), Some(_), false) => self.type_range(element_ty.clone()),
            (Some(_), Some(_), true) => self.type_range_inclusive(element_ty.clone()),
            (Some(_), None, _) => self.type_range_from(element_ty.clone()),
            (None, Some(_), false) => self.type_range_to(element_ty.clone()),
            (None, Some(_), true) => self.type_range_to_inclusive(element_ty.clone()),
            (None, None, _) => {
                self.sink
                    .push(diagnostics::infer::range_full_not_valid_expression(span));
                let error_ty = self.new_type_var();
                self.type_range(error_ty)
            }
        };

        self.unify(expected_ty, &range_ty, &span);

        Expression::Range {
            start: new_start,
            end: new_end,
            inclusive,
            ty: range_ty,
            span,
        }
    }

    pub(super) fn infer_cast(
        &mut self,
        expression: Box<Expression>,
        target_type: syntax::ast::Annotation,
        span: Span,
        expected_ty: &Type,
    ) -> Expression {
        let target_ty = self.convert_to_type(&target_type, &span);

        let source_ty_var = self.new_type_var();
        let new_expression =
            self.with_value_context(|s| s.infer_expression(*expression, &source_ty_var));
        let source_ty = source_ty_var.resolve();

        self.check_not_temp_producing(&new_expression);

        if is_cast_expression(&new_expression) {
            self.sink.push(diagnostics::infer::chained_cast(span));
        }

        if !self.check_redundant_cast(&source_ty, &target_ty, span) {
            self.check_redundant_literal_cast(&new_expression, &target_ty, expected_ty, span);
        }

        self.check_cast_literal_overflow(&new_expression, &target_ty, span);

        self.check_valid_cast(&source_ty, &target_ty, span);

        if is_float_literal(&new_expression) && is_integer_type(&target_ty) {
            self.sink
                .push(diagnostics::infer::float_literal_int_cast(span));
        }

        self.unify(expected_ty, &target_ty, &span);

        Expression::Cast {
            expression: new_expression.into(),
            target_type,
            ty: target_ty,
            span,
        }
    }
}

fn is_float_literal(expression: &Expression) -> bool {
    match expression.unwrap_parens() {
        Expression::Literal {
            literal: Literal::Float { .. },
            ..
        } => true,
        Expression::Unary {
            operator: Negative,
            expression,
            ..
        } => is_float_literal(expression),
        _ => false,
    }
}

fn is_integer_type(ty: &Type) -> bool {
    matches!(
        ty.resolve().get_name(),
        Some(
            "int"
                | "int8"
                | "int16"
                | "int32"
                | "int64"
                | "uint"
                | "uint8"
                | "uint16"
                | "uint32"
                | "uint64"
                | "byte"
                | "rune"
        )
    )
}

fn is_cast_expression(expression: &Expression) -> bool {
    match expression {
        Expression::Cast { .. } => true,
        Expression::Paren { expression, .. } => is_cast_expression(expression),
        _ => false,
    }
}

fn is_numeric_literal(expression: &Expression) -> bool {
    match expression {
        Expression::Literal {
            literal: Literal::Integer { .. } | Literal::Float { .. },
            ..
        } => true,
        Expression::Paren { expression, .. } => is_numeric_literal(expression),
        _ => false,
    }
}

/// Distinguishes integer vs float literals for type adaptation purposes.
enum NumericLiteralKind {
    /// Integer literal: adapts to any numeric type (is_numeric)
    Integer,
    /// Float literal: adapts only to float types (is_float)
    Float,
    /// Not a numeric literal
    None,
}

fn numeric_literal_kind(expression: &Expression) -> NumericLiteralKind {
    match expression {
        Expression::Literal {
            literal: Literal::Integer { .. },
            ..
        } => NumericLiteralKind::Integer,
        Expression::Literal {
            literal: Literal::Float { .. },
            ..
        } => NumericLiteralKind::Float,
        Expression::Paren { expression, .. } => numeric_literal_kind(expression),
        _ => NumericLiteralKind::None,
    }
}

/// Checks whether a literal of the given kind can adapt to the target type.
fn literal_can_adapt_to(kind: &NumericLiteralKind, target: &Type) -> bool {
    match kind {
        NumericLiteralKind::Integer => target.is_numeric(),
        NumericLiteralKind::Float => target.is_float(),
        NumericLiteralKind::None => false,
    }
}