use vecmap in PatternState

This commit is contained in:
Folkert 2022-05-22 15:58:32 +02:00
parent befa202e0a
commit b2a9911f70
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
2 changed files with 28 additions and 20 deletions

View file

@ -19,6 +19,7 @@ use roc_can::pattern::Pattern;
use roc_can::traverse::symbols_introduced_from_pattern; use roc_can::traverse::symbols_introduced_from_pattern;
use roc_collections::all::{HumanIndex, MutMap, SendMap}; use roc_collections::all::{HumanIndex, MutMap, SendMap};
use roc_collections::soa::Index; use roc_collections::soa::Index;
use roc_collections::VecMap;
use roc_module::ident::{Lowercase, TagName}; use roc_module::ident::{Lowercase, TagName};
use roc_module::symbol::{ModuleId, Symbol}; use roc_module::symbol::{ModuleId, Symbol};
use roc_region::all::{Loc, Region}; use roc_region::all::{Loc, Region};
@ -743,14 +744,20 @@ pub fn constrain_expr(
); );
pattern_vars.extend(new_pattern_vars); pattern_vars.extend(new_pattern_vars);
debug_assert!(
pattern_headers if cfg!(debug_assertions) {
.clone() let intersection: Vec<_> = pattern_headers
.intersection(new_pattern_headers.clone()) .keys()
.is_empty(), .filter(|k| new_pattern_headers.contains_key(k))
"Two patterns introduce the same symbols - that's a bug!\n{:?}", .collect();
pattern_headers.clone().intersection(new_pattern_headers)
); debug_assert!(
intersection.is_empty(),
"Two patterns introduce the same symbols - that's a bug!\n{:?}",
intersection
);
}
pattern_headers.extend(new_pattern_headers); pattern_headers.extend(new_pattern_headers);
pattern_cons.push(pattern_con); pattern_cons.push(pattern_con);
@ -1239,7 +1246,7 @@ fn constrain_function_def(
let loc_body_expr = loc_expr; let loc_body_expr = loc_expr;
let mut argument_pattern_state = PatternState { let mut argument_pattern_state = PatternState {
headers: SendMap::default(), headers: VecMap::default(),
vars: Vec::with_capacity(function_def.arguments.len()), vars: Vec::with_capacity(function_def.arguments.len()),
constraints: Vec::with_capacity(1), constraints: Vec::with_capacity(1),
delayed_is_open_constraints: vec![], delayed_is_open_constraints: vec![],
@ -1626,7 +1633,7 @@ fn constrain_when_branch_help(
expr_expected: Expected<Type>, expr_expected: Expected<Type>,
) -> ( ) -> (
Vec<Variable>, Vec<Variable>,
SendMap<Symbol, Loc<Type>>, VecMap<Symbol, Loc<Type>>,
Constraint, Constraint,
Constraint, Constraint,
) { ) {
@ -1639,7 +1646,7 @@ fn constrain_when_branch_help(
); );
let mut state = PatternState { let mut state = PatternState {
headers: SendMap::default(), headers: VecMap::default(),
vars: Vec::with_capacity(2), vars: Vec::with_capacity(2),
constraints: Vec::with_capacity(2), constraints: Vec::with_capacity(2),
delayed_is_open_constraints: Vec::new(), delayed_is_open_constraints: Vec::new(),
@ -1821,7 +1828,7 @@ pub(crate) fn constrain_def_pattern(
let pattern_expected = PExpected::NoExpectation(expr_type); let pattern_expected = PExpected::NoExpectation(expr_type);
let mut state = PatternState { let mut state = PatternState {
headers: SendMap::default(), headers: VecMap::default(),
vars: Vec::with_capacity(1), vars: Vec::with_capacity(1),
constraints: Vec::with_capacity(1), constraints: Vec::with_capacity(1),
delayed_is_open_constraints: vec![], delayed_is_open_constraints: vec![],
@ -1920,7 +1927,7 @@ fn constrain_typed_def(
let loc_body_expr = &**loc_body; let loc_body_expr = &**loc_body;
let mut argument_pattern_state = PatternState { let mut argument_pattern_state = PatternState {
headers: SendMap::default(), headers: VecMap::default(),
vars: Vec::with_capacity(arguments.len()), vars: Vec::with_capacity(arguments.len()),
constraints: Vec::with_capacity(1), constraints: Vec::with_capacity(1),
delayed_is_open_constraints: vec![], delayed_is_open_constraints: vec![],
@ -2487,7 +2494,7 @@ fn instantiate_rigids(
introduced_vars: &IntroducedVariables, introduced_vars: &IntroducedVariables,
loc_pattern: &Loc<Pattern>, loc_pattern: &Loc<Pattern>,
ftv: &mut MutMap<Lowercase, Variable>, // rigids defined before the current annotation ftv: &mut MutMap<Lowercase, Variable>, // rigids defined before the current annotation
headers: &mut SendMap<Symbol, Loc<Type>>, headers: &mut VecMap<Symbol, Loc<Type>>,
) -> InstantiateRigids { ) -> InstantiateRigids {
let mut annotation = annotation.clone(); let mut annotation = annotation.clone();
let mut new_rigid_variables: Vec<Variable> = Vec::new(); let mut new_rigid_variables: Vec<Variable> = Vec::new();
@ -2700,7 +2707,7 @@ fn constraint_recursive_function(
let loc_body_expr = loc_expr; let loc_body_expr = loc_expr;
let mut argument_pattern_state = PatternState { let mut argument_pattern_state = PatternState {
headers: SendMap::default(), headers: VecMap::default(),
vars: Vec::with_capacity(function_def.arguments.len()), vars: Vec::with_capacity(function_def.arguments.len()),
constraints: Vec::with_capacity(1), constraints: Vec::with_capacity(1),
delayed_is_open_constraints: vec![], delayed_is_open_constraints: vec![],
@ -3103,7 +3110,7 @@ pub fn rec_defs_help(
let loc_body_expr = &**loc_body; let loc_body_expr = &**loc_body;
let mut state = PatternState { let mut state = PatternState {
headers: SendMap::default(), headers: VecMap::default(),
vars: Vec::with_capacity(arguments.len()), vars: Vec::with_capacity(arguments.len()),
constraints: Vec::with_capacity(1), constraints: Vec::with_capacity(1),
delayed_is_open_constraints: vec![], delayed_is_open_constraints: vec![],

View file

@ -5,6 +5,7 @@ use roc_can::expected::{Expected, PExpected};
use roc_can::pattern::Pattern::{self, *}; use roc_can::pattern::Pattern::{self, *};
use roc_can::pattern::{DestructType, RecordDestruct}; use roc_can::pattern::{DestructType, RecordDestruct};
use roc_collections::all::{HumanIndex, SendMap}; use roc_collections::all::{HumanIndex, SendMap};
use roc_collections::VecMap;
use roc_module::ident::Lowercase; use roc_module::ident::Lowercase;
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use roc_region::all::{Loc, Region}; use roc_region::all::{Loc, Region};
@ -16,7 +17,7 @@ use roc_types::types::{
#[derive(Default)] #[derive(Default)]
pub struct PatternState { pub struct PatternState {
pub headers: SendMap<Symbol, Loc<Type>>, pub headers: VecMap<Symbol, Loc<Type>>,
pub vars: Vec<Variable>, pub vars: Vec<Variable>,
pub constraints: Vec<Constraint>, pub constraints: Vec<Constraint>,
pub delayed_is_open_constraints: Vec<Constraint>, pub delayed_is_open_constraints: Vec<Constraint>,
@ -32,8 +33,8 @@ pub struct PatternState {
pub fn headers_from_annotation( pub fn headers_from_annotation(
pattern: &Pattern, pattern: &Pattern,
annotation: &Loc<&Type>, annotation: &Loc<&Type>,
) -> Option<SendMap<Symbol, Loc<Type>>> { ) -> Option<VecMap<Symbol, Loc<Type>>> {
let mut headers = SendMap::default(); let mut headers = VecMap::default();
// Check that the annotation structurally agrees with the pattern, preventing e.g. `{ x, y } : Int` // Check that the annotation structurally agrees with the pattern, preventing e.g. `{ x, y } : Int`
// in such incorrect cases we don't put the full annotation in headers, just a variable, and let // in such incorrect cases we don't put the full annotation in headers, just a variable, and let
// inference generate a proper error. // inference generate a proper error.
@ -49,7 +50,7 @@ pub fn headers_from_annotation(
fn headers_from_annotation_help( fn headers_from_annotation_help(
pattern: &Pattern, pattern: &Pattern,
annotation: &Loc<&Type>, annotation: &Loc<&Type>,
headers: &mut SendMap<Symbol, Loc<Type>>, headers: &mut VecMap<Symbol, Loc<Type>>,
) -> bool { ) -> bool {
match pattern { match pattern {
Identifier(symbol) Identifier(symbol)