From ad7fb5d0fcbd620d67fceb0bdd0bd949e548cb9e Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Mon, 11 Nov 2024 22:34:42 +0100 Subject: [PATCH] adding errors for useless type hints --- lang/README.md | 6 +++--- lang/src/son.rs | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lang/README.md b/lang/README.md index 50c0c55..2442126 100644 --- a/lang/README.md +++ b/lang/README.md @@ -646,7 +646,7 @@ main := fn(): int { i -= 1 } // b g r - put_pixel(.(x, y), .(@as(u8, @intcast(i)), @as(u8, @intcast(i)), @as(u8, @intcast(i)), 255)) + put_pixel(.(x, y), .(@intcast(i), @intcast(i), @intcast(i), 255)) } } @@ -1030,7 +1030,7 @@ main := fn(): uint { ```hb Color := struct {r: u8, g: u8, b: u8, a: u8} white := Color.(255, 255, 255, 255) -u32_to_color := fn(v: u32): Color return @as(Color, @bitcast(u32_to_u32(@bitcast(v)))) +u32_to_color := fn(v: u32): Color return @bitcast(u32_to_u32(@bitcast(v))) u32_to_u32 := fn(v: u32): u32 return v main := fn(): uint { return u32_to_color(@bitcast(white)).r @@ -1399,7 +1399,7 @@ main := fn(): uint { } n = 1 loop if n >= 10 break else { - *(@as(^[u8; 1024], @bitcast(&back_buffer)) + n) = *@as(^[u8; 1024], @bitcast(&back_buffer)) + *(@as(^[u8; 1024], @bitcast(&back_buffer)) + n) = *@bitcast(&back_buffer) n += 1 } return back_buffer[1024 * 2] diff --git a/lang/src/son.rs b/lang/src/son.rs index 52ba57a..1d1d7bc 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -3168,10 +3168,16 @@ impl<'a> Codegen<'a> { self.tys, )) } - Expr::Directive { name: "as", args: [ty, expr], .. } => { + Expr::Directive { name: "as", args: [ty, expr], pos } => { let ty = self.ty(ty); - let ctx = Ctx::default().with_ty(ty); - let mut val = self.raw_expr_ctx(expr, ctx)?; + let mut val = self.raw_expr_ctx(expr, Ctx::default().with_ty(ty))?; + + if let Some(ity) = ctx.ty + && ity.try_upcast(ty) == Some(ty) + && val.ty == ity + { + self.report(pos, "the type is known at this point, remove the hint"); + } self.strip_var(&mut val); self.assert_ty(expr.pos(), &mut val, ty, "hinted expr"); Some(val)