Skip to content

Commit

Permalink
Merge pull request #1084 from hash-org/parsing-ty-args
Browse files Browse the repository at this point in the history
  • Loading branch information
feds01 authored Dec 22, 2024
2 parents bcf736e + 68b5514 commit 3d45d67
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
15 changes: 5 additions & 10 deletions compiler/hash-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,15 @@ static SPAN_MAP: Lazy<RwLock<Vec<Span>>> = Lazy::new(|| {
/// added to the global [`SPAN_MAP`] later.
///
/// ##Note: This is only used by the parser in order to reduce contention for [`SPAN_MAP`].
#[derive(Default)]
pub struct LocalSpanMap {
map: Vec<(AstNodeId, ByteRange)>,
source: SourceId,
}

impl LocalSpanMap {
/// Create a new [LocalSpanMap].
pub fn new(source: SourceId) -> Self {
Self { map: vec![], source }
}

/// Create a new [LocalSpanMap] with a given capacity.
pub fn with_capacity(source: SourceId, capacity: usize) -> Self {
Self { map: Vec::with_capacity(capacity), source }
pub fn with_capacity(capacity: usize) -> Self {
Self { map: Vec::with_capacity(capacity) }
}

/// Add a new node to the map.
Expand Down Expand Up @@ -156,7 +151,7 @@ impl SpanMap {
}

/// Merge a [LocalSpanMap] into the [`SPAN_MAP`].
pub fn add_local_map(local: LocalSpanMap) {
pub fn add_local_map(source: SourceId, local: LocalSpanMap) {
// If no nodes were added, don't do anything!
if local.map.is_empty() {
return;
Expand All @@ -173,7 +168,7 @@ impl SpanMap {

// Now we write all of the items into the map.
for (id, range) in local.map {
writer[id.to_usize()] = Span::new(range, local.source);
writer[id.to_usize()] = Span::new(range, source);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/hash-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ impl<Ctx: ParserCtxQuery> CompilerStage<Ctx> for Parser {
let path = SourceMapUtils::map(id, |source| source.path().to_path_buf());
node_map.add_module(id, ModuleEntry::new(path, node));
}
ParserAction::MergeSpans { spans } => scope.spawn(move |_| {
SpanMap::add_local_map(spans);
ParserAction::MergeSpans { source, spans } => scope.spawn(move |_| {
SpanMap::add_local_map(source, spans);
}),
ParserAction::ParseImport { source, sender } => {
// ##Note: import de-duplication is already ensured by the
Expand Down Expand Up @@ -172,7 +172,7 @@ pub enum ParserAction {
Error { diagnostics: Vec<Report>, timings: StageMetrics },

/// Update the global `SPAN_MAP` with the specified local span map.
MergeSpans { spans: LocalSpanMap },
MergeSpans { source: SourceId, spans: LocalSpanMap },

/// A worker has completed processing an interactive block and now provides
/// the generated AST.
Expand Down Expand Up @@ -266,7 +266,7 @@ fn parse_source(source: ParseSource, sender: Sender<ParserAction>) {
// are encountered whilst parsing this module.
let resolver = ImportResolver::new(id, source.parent(), sender);
let mut diagnostics = ParserDiagnostics::new();
let mut spans = LocalSpanMap::with_capacity(id, tokens.len() * 2);
let mut spans = LocalSpanMap::with_capacity(tokens.len() * 2);
let mut gen = AstGen::new(spanned, &tokens, &resolver, &mut diagnostics, &mut spans);

// Perform the parsing operation now... and send the result through the
Expand Down Expand Up @@ -299,6 +299,6 @@ fn parse_source(source: ParseSource, sender: Sender<ParserAction>) {

// Send both the generated module, and the `LocalSpanMap` for updating
// the global `SPAN_MAP`.
sender.send(ParserAction::MergeSpans { spans }).unwrap();
sender.send(ParserAction::MergeSpans { source: id, spans }).unwrap();
sender.send(action).unwrap();
}
6 changes: 0 additions & 6 deletions compiler/hash-parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ impl<'s> AstGenFrame<'s> {
let pos = self.previous_pos().end() + 1;
ByteRange::new(pos, pos)
}

/// Check whether the frame has encountered an error.
#[inline(always)]
pub(crate) fn has_error(&self) -> bool {
self.error.get()
}
}

/// The [AstGen] struct it the primary parser for the Hash compiler. It
Expand Down
4 changes: 2 additions & 2 deletions compiler/hash-parser/src/parser/ty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Hash Compiler AST generation sources. This file contains the sources to the
//! logic that transforms tokens into an AST.
use hash_ast::ast::*;
use hash_reporting::diagnostic::HasDiagnosticsMut;
use hash_reporting::diagnostic::{DiagnosticsMut, HasDiagnosticsMut};
use hash_source::identifier::IDENTS;
use hash_token::{delimiter::Delimiter, keyword::Keyword, Token, TokenKind};
use hash_utils::thin_vec::thin_vec;
Expand Down Expand Up @@ -331,7 +331,7 @@ impl AstGen<'_> {
// Abort early if we encountered some kind of error along the way,
// although I would think when the `gen` is consumed then we can
// send up all of the errors to the parent generator?
if gen.has_error() {
if gen.diagnostics.has_errors() {
let err = gen.diagnostics.errors.pop().unwrap();
return Err(err);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/parser/types/tuple_type_args.hash
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run=pass, stage=parse

set := (data: char) -> Result<void, IoError> => {
match (intrinsic_char_set(data) as Result<void, (i32, str)>) {
Err(e) => Err(_conv_ioerr_prim(e)),
Ok(r) => Ok(r)
}
};

0 comments on commit 3d45d67

Please sign in to comment.