From 7c040715c2df6365a641b2f625237b687793b833 Mon Sep 17 00:00:00 2001 From: Lennart Van Hirtum Date: Sun, 7 Apr 2024 15:16:32 +0200 Subject: [PATCH] Warning fixing, and improve Bracket Span --- src/dev_aid/syntax_highlighting.rs | 2 +- src/file_position.rs | 53 +++--------------------------- src/flattening/mod.rs | 12 +++---- src/instantiation/mod.rs | 4 +-- src/typing.rs | 10 +++--- 5 files changed, 18 insertions(+), 63 deletions(-) diff --git a/src/dev_aid/syntax_highlighting.rs b/src/dev_aid/syntax_highlighting.rs index 2095520..b732d1b 100644 --- a/src/dev_aid/syntax_highlighting.rs +++ b/src/dev_aid/syntax_highlighting.rs @@ -35,7 +35,7 @@ pub fn walk_name_color(all_objects : &[NameElem], linker : &Linker) -> Vec<(IDEI } WireSource::UnaryOp { op:_, right:_ } => {} WireSource::BinaryOp { op:_, left:_, right:_ } => {} - WireSource::ArrayAccess { arr:_, arr_idx:_ } => {} + WireSource::ArrayAccess { arr:_, arr_idx:_, bracket_span:_ } => {} WireSource::Constant(_) => {} WireSource::NamedConstant(_name) => { result.push((IDEIdentifierType::Constant, w.span)); diff --git a/src/file_position.rs b/src/file_position.rs index 34e5f73..ff3041e 100644 --- a/src/file_position.rs +++ b/src/file_position.rs @@ -15,7 +15,6 @@ impl From> for Span { impl Span { /// Only really used for having a span with the maximum size. pub const MAX_POSSIBLE_SPAN : Span = Span(0, usize::MAX); - pub const INVALID_SPAN : Span = Span(usize::MAX, usize::MAX); pub fn into_range(&self) -> Range { self.0..self.1 @@ -33,33 +32,6 @@ impl Span { pub fn size(&self) -> usize { self.1 - self.0 } - #[track_caller] - pub fn difference_left(outer : Span, inner : Span) -> Span { - assert!(outer.0 <= inner.0); - assert!(outer.1 >= inner.1); - - Span(outer.0, inner.0) - } - #[track_caller] - pub fn shrunk_front(outer : Span, inner : Span) -> Span { - assert!(outer.0 <= inner.0); - assert!(outer.1 >= inner.1); - - Span(inner.0, outer.1) - } - #[track_caller] - pub fn difference_right(outer : Span, inner : Span) -> Span { - assert!(outer.0 <= inner.0); - assert!(outer.1 >= inner.1); - - Span(inner.1, outer.1) - } - #[track_caller] - pub fn into_single_char_span(self) -> SingleCharSpan { - assert!(self.1 == self.0+1); - SingleCharSpan{char_idx: self.0} - } - pub fn empty_span_at_front(self) -> Span { Span(self.0, self.0) } @@ -93,28 +65,11 @@ impl BracketSpan { pub fn outer_span(&self) -> Span { self.0 } - pub fn open_bracket(&self) -> SingleCharSpan { - SingleCharSpan{char_idx : self.0.0} - } - pub fn close_bracket(&self) -> SingleCharSpan { - SingleCharSpan{char_idx : self.0.1 - 1} - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub struct SingleCharSpan { - pub char_idx : usize -} - -impl Into for SingleCharSpan { - fn into(self) -> Span { - Span(self.char_idx, self.char_idx+1) + pub fn open_bracket(&self) -> Span { + Span(self.0.0, self.0.0+1) } -} - -impl Into for &SingleCharSpan { - fn into(self) -> Span { - Span(self.char_idx, self.char_idx+1) + pub fn close_bracket(&self) -> Span { + Span(self.0.1 - 1, self.0.1) } } diff --git a/src/flattening/mod.rs b/src/flattening/mod.rs index 6fb8806..64fa35a 100644 --- a/src/flattening/mod.rs +++ b/src/flattening/mod.rs @@ -171,7 +171,7 @@ pub enum WireSource { WireRead(FlatID), // Used to add a span to the reference of a wire. UnaryOp{op : UnaryOperator, right : FlatID}, BinaryOp{op : BinaryOperator, left : FlatID, right : FlatID}, - ArrayAccess{arr : FlatID, arr_idx : FlatID}, + ArrayAccess{arr : FlatID, arr_idx : FlatID, bracket_span : BracketSpan}, Constant(Value), NamedConstant(ConstantUUID), } @@ -182,7 +182,7 @@ impl WireSource { &WireSource::WireRead(from_wire) => {func(from_wire)} &WireSource::UnaryOp { op:_, right } => {func(right)} &WireSource::BinaryOp { op:_, left, right } => {func(left); func(right)} - &WireSource::ArrayAccess { arr, arr_idx } => {func(arr); func(arr_idx)} + &WireSource::ArrayAccess { arr, arr_idx, bracket_span:_ } => {func(arr); func(arr_idx)} WireSource::Constant(_) => {} WireSource::NamedConstant(_) => {} } @@ -352,7 +352,7 @@ impl<'l> FlatteningContext<'l> { let (array_size_wire_id, bracket_span) = cursor.field(SUS.arr_idx_field, |cursor| self.flatten_array_bracket_tree(cursor)); - WrittenType::Array(span, Box::new((array_element_type, array_size_wire_id))) + WrittenType::Array(span, Box::new((array_element_type, array_size_wire_id, bracket_span))) }) } @@ -495,7 +495,7 @@ impl<'l> FlatteningContext<'l> { args = &args[..expected_arg_count]; } else { // Too few args, mention missing argument names - self.errors.error_with_info(arguments_span.close_bracket().into(), format!("Too few arguments. Function takes {expected_arg_count} args, but {arg_count} were passed."), module_info); + self.errors.error_with_info(arguments_span.close_bracket(), format!("Too few arguments. Function takes {expected_arg_count} args, but {arg_count} were passed."), module_info); } } @@ -584,7 +584,7 @@ impl<'l> FlatteningContext<'l> { let (arr_idx, bracket_span) = cursor.field(SUS.arr_idx_field, |cursor| self.flatten_array_bracket_tree(cursor)); - WireSource::ArrayAccess{arr, arr_idx} + WireSource::ArrayAccess{arr, arr_idx, bracket_span} }) } else if kind == SUS.func_call_kind { if let Some((_module_name_span, md, interface_wires)) = self.desugar_func_call_tree(cursor) { @@ -991,7 +991,7 @@ impl<'l> FlatteningContext<'l> { self.typecheck_wire_is_of_type(right_wire, &input_right_type, &format!("{op} right")); output_type } - &WireSource::ArrayAccess{arr, arr_idx} => { + &WireSource::ArrayAccess{arr, arr_idx, bracket_span:_} => { let arr_wire = self.instructions[arr].extract_wire(); let arr_idx_wire = self.instructions[arr_idx].extract_wire(); diff --git a/src/instantiation/mod.rs b/src/instantiation/mod.rs index f5dae0d..fb3dc7c 100644 --- a/src/instantiation/mod.rs +++ b/src/instantiation/mod.rs @@ -322,7 +322,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> { let right_val = self.get_generation_value(right)?; compute_binary_op(left_val, op, right_val) } - &WireSource::ArrayAccess{arr, arr_idx} => { + &WireSource::ArrayAccess{arr, arr_idx, bracket_span:_} => { let Value::Array(arr_val) = self.get_generation_value(arr)? else {return None}; let arr_idx_wire = self.flattened.instructions[arr_idx].extract_wire(); let idx : usize = self.extract_integer_from_generative(arr_idx)?; @@ -372,7 +372,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> { let right = self.get_wire_or_constant_as_wire(right)?; RealWireDataSource::BinaryOp{op, left, right} } - &WireSource::ArrayAccess{arr, arr_idx} => { + &WireSource::ArrayAccess{arr, arr_idx, bracket_span:_} => { let arr = self.get_wire_or_constant_as_wire(arr)?; match &self.generation_state[arr_idx] { SubModuleOrWire::SubModule(_) => unreachable!(), diff --git a/src/typing.rs b/src/typing.rs index 410e7c4..b607a96 100644 --- a/src/typing.rs +++ b/src/typing.rs @@ -1,13 +1,13 @@ use std::ops::Deref; -use crate::{arena_alloc::ArenaAllocator, errors::ErrorCollector, file_position::Span, flattening::{BinaryOperator, FlatID, UnaryOperator}, linker::{get_builtin_type, Linkable, Linker, NamedType, TypeUUID, TypeUUIDMarker}, value::Value}; +use crate::{arena_alloc::ArenaAllocator, errors::ErrorCollector, file_position::{BracketSpan, Span}, flattening::{BinaryOperator, FlatID, UnaryOperator}, linker::{get_builtin_type, Linkable, Linker, NamedType, TypeUUID, TypeUUIDMarker}, value::Value}; // These are #[derive(Debug, Clone)] pub enum WrittenType { Error(Span), Named(Span, TypeUUID), - Array(Span, Box<(WrittenType, FlatID)>) + Array(Span, Box<(WrittenType, FlatID, BracketSpan)>) } impl WrittenType { @@ -16,7 +16,7 @@ impl WrittenType { WrittenType::Error(span) => {f(None, *span)} WrittenType::Named(span, id) => {f(Some(*id), *span)} WrittenType::Array(_span, arr_box) => { - let (arr, _idx) = arr_box.deref(); + let (arr, _idx, _br_span) = arr_box.deref(); arr.for_each_located_type(f); } } @@ -34,7 +34,7 @@ impl WrittenType { match self { WrittenType::Error(_span) | WrittenType::Named(_span, _) => {} WrittenType::Array(_span, arr_box) => { - let (arr_typ, _idx) = arr_box.deref(); + let (arr_typ, _idx, _br_span) = arr_box.deref(); let sub = arr_typ.get_deepest_selected(position); if sub.is_some() { return sub; @@ -52,7 +52,7 @@ impl WrittenType { WrittenType::Error(_) => Type::Error, WrittenType::Named(_, id) => Type::Named(*id), WrittenType::Array(_, arr_box) => { - let (elem_typ, arr_idx) = arr_box.deref(); + let (elem_typ, arr_idx, _br_span) = arr_box.deref(); Type::Array(Box::new((elem_typ.to_type(), *arr_idx))) } }