From 597a1cef3be09fb1bc17fab8d759d69eeb3467a3 Mon Sep 17 00:00:00 2001 From: ayazhafiz Date: Sun, 26 Dec 2021 09:17:27 -0600 Subject: [PATCH] Attach location to alias header name --- ast/src/lang/core/types.rs | 2 +- compiler/can/src/annotation.rs | 2 +- compiler/fmt/src/annotation.rs | 2 +- compiler/parse/src/ast.rs | 2 +- compiler/parse/src/type_annotation.rs | 32 ++++++++++++--------------- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/ast/src/lang/core/types.rs b/ast/src/lang/core/types.rs index 7b45b77bd7..f7aa8aeb80 100644 --- a/ast/src/lang/core/types.rs +++ b/ast/src/lang/core/types.rs @@ -461,7 +461,7 @@ pub fn to_type2<'a>( ) => { // e.g. `{ x : Int, y : Int } as Point` let symbol = match scope.introduce( - (*name).into(), + name.value.into(), &env.exposed_ident_ids, &mut env.ident_ids, region, diff --git a/compiler/can/src/annotation.rs b/compiler/can/src/annotation.rs index 92db39f82b..03bc4125d2 100644 --- a/compiler/can/src/annotation.rs +++ b/compiler/can/src/annotation.rs @@ -383,7 +383,7 @@ fn can_annotation_help( }, ) => { let symbol = match scope.introduce( - (*name).into(), + name.value.into(), &env.exposed_ident_ids, &mut env.ident_ids, region, diff --git a/compiler/fmt/src/annotation.rs b/compiler/fmt/src/annotation.rs index da23f997b4..d3b2ccae99 100644 --- a/compiler/fmt/src/annotation.rs +++ b/compiler/fmt/src/annotation.rs @@ -251,7 +251,7 @@ impl<'a> Formattable for TypeAnnotation<'a> { buf.spaces(1); buf.push_str("as"); buf.spaces(1); - buf.push_str(name); + buf.push_str(name.value); for var in *vars { buf.spaces(1); buf.push_str(var.value); diff --git a/compiler/parse/src/ast.rs b/compiler/parse/src/ast.rs index da622d102a..2cc737809f 100644 --- a/compiler/parse/src/ast.rs +++ b/compiler/parse/src/ast.rs @@ -227,7 +227,7 @@ pub struct PrecedenceConflict<'a> { #[derive(Debug, Clone, Copy, PartialEq)] pub struct AliasHeader<'a> { - pub name: &'a str, + pub name: Loc<&'a str>, pub vars: &'a [Loc<&'a str>], } diff --git a/compiler/parse/src/type_annotation.rs b/compiler/parse/src/type_annotation.rs index 23285cde9b..04d3ba7be7 100644 --- a/compiler/parse/src/type_annotation.rs +++ b/compiler/parse/src/type_annotation.rs @@ -9,7 +9,7 @@ use crate::parser::{ use crate::state::State; use bumpalo::collections::vec::Vec; use bumpalo::Bump; -use roc_region::all::{Loc, Position}; +use roc_region::all::{Loc, Position, Region}; pub fn located_help<'a>( min_indent: u16, @@ -50,7 +50,7 @@ fn tag_union_type<'a>(min_indent: u16) -> impl Parser<'a, TypeAnnotation<'a>, ET fn check_type_alias( p: Progress, annot: Loc, -) -> impl Parser, ETypeInlineAlias> { +) -> impl Parser { move |arena, state| match annot.value { TypeAnnotation::Apply("", tag_name, vars) => { let mut var_names = Vec::new_in(arena); @@ -67,12 +67,16 @@ fn check_type_alias( } } + let name_start = annot.region.start(); + let name_region = + Region::between(name_start, name_start.bump_column(tag_name.len() as u16)); + let header = AliasHeader { - name: tag_name, + name: Loc::at(name_region, tag_name), vars: var_names.into_bump_slice(), }; - Ok((p, Loc::at(annot.region, header), state)) + Ok((p, header, state)) } TypeAnnotation::Apply(_, _, _) => { Err((p, ETypeInlineAlias::Qualified(annot.region.start()), state)) @@ -81,9 +85,7 @@ fn check_type_alias( } } -fn parse_type_alias_after_as<'a>( - min_indent: u16, -) -> impl Parser<'a, Loc>, EType<'a>> { +fn parse_type_alias_after_as<'a>(min_indent: u16) -> impl Parser<'a, AliasHeader<'a>, EType<'a>> { move |arena, state| { space0_before_e( term(min_indent), @@ -131,18 +133,12 @@ fn term<'a>(min_indent: u16) -> impl Parser<'a, Loc>, EType<' ] ), |arena: &'a Bump, - (loc_ann, opt_as): ( - Loc>, - Option<(&'a [_], Loc>)> - )| { + (loc_ann, opt_as): (Loc>, Option<(&'a [_], AliasHeader<'a>)>)| { match opt_as { - Some(( - spaces, - Loc { - region, - value: alias, - }, - )) => { + Some((spaces, alias)) => { + let alias_vars_region = + Region::across_all(alias.vars.into_iter().map(|v| &v.region)); + let region = Region::span_across(&loc_ann.region, &alias_vars_region); let value = TypeAnnotation::As(arena.alloc(loc_ann), spaces, alias); Loc { region, value }