Thread error message information for requires

This commit is contained in:
Ayaz Hafiz 2022-05-05 16:52:58 -04:00 committed by Richard Feldman
parent c7142da116
commit 77a1f644a4
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
3 changed files with 22 additions and 14 deletions

View file

@ -3,13 +3,14 @@ use roc_can::abilities::AbilitiesStore;
use roc_can::constraint::{Constraint, Constraints};
use roc_can::def::Declaration;
use roc_can::expected::Expected;
use roc_can::pattern::Pattern;
use roc_collections::all::MutMap;
use roc_error_macros::internal_error;
use roc_module::symbol::{ModuleId, Symbol};
use roc_region::all::{Loc, Region};
use roc_types::solved_types::{FreeVars, SolvedType};
use roc_types::subs::{VarStore, Variable};
use roc_types::types::{Category, Type};
use roc_types::types::{AnnotationSource, Category, Type};
use crate::expr::{constrain_def_make_constraint, constrain_def_pattern, Env};
@ -96,7 +97,7 @@ pub enum ExposedModuleTypes {
pub fn constrain_module(
constraints: &mut Constraints,
symbols_from_requires: Vec<(Symbol, Loc<Type>)>,
symbols_from_requires: Vec<(Loc<Symbol>, Loc<Type>)>,
abilities_store: &AbilitiesStore,
declarations: &[Declaration],
home: ModuleId,
@ -114,14 +115,14 @@ pub fn constrain_module(
fn constrain_symbols_from_requires(
constraints: &mut Constraints,
symbols_from_requires: Vec<(Symbol, Loc<Type>)>,
symbols_from_requires: Vec<(Loc<Symbol>, Loc<Type>)>,
home: ModuleId,
constraint: Constraint,
) -> Constraint {
symbols_from_requires
.into_iter()
.fold(constraint, |constraint, (symbol, loc_type)| {
if symbol.module_id() == home {
.fold(constraint, |constraint, (loc_symbol, loc_type)| {
if loc_symbol.value.module_id() == home {
// 1. Required symbols can only be specified in package modules
// 2. Required symbols come from app modules
// But, if we are running e.g. `roc check` on a package module, there is no app
@ -130,7 +131,7 @@ fn constrain_symbols_from_requires(
// the types they are annotated with.
let rigids = Default::default();
let env = Env { home, rigids };
let pattern = Loc::at_zero(roc_can::pattern::Pattern::Identifier(symbol));
let pattern = Loc::at_zero(roc_can::pattern::Pattern::Identifier(loc_symbol.value));
let def_pattern_state =
constrain_def_pattern(constraints, &env, &pattern, loc_type.value.clone());
@ -148,10 +149,17 @@ fn constrain_symbols_from_requires(
} else {
// Otherwise, this symbol comes from an app module - we want to check that the type
// provided by the app is in fact what the package module requires.
let arity = loc_type.value.arity();
let provided_eq_requires_constr = constraints.lookup(
symbol,
// TODO give it a real expectation, so errors can be helpful
Expected::NoExpectation(loc_type.value),
loc_symbol.value,
Expected::FromAnnotation(
loc_symbol.map(|&s| Pattern::Identifier(s)),
arity,
AnnotationSource::TypedBody {
region: loc_type.region,
},
loc_type.value,
),
loc_type.region,
);
constraints.and_constraint([provided_eq_requires_constr, constraint])