Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align PNC Apply Pattern with Expr #7490

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 63 additions & 13 deletions crates/compiler/can/src/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1377,10 +1377,11 @@ fn desugar_pattern<'a>(env: &mut Env<'a>, scope: &mut Scope, pattern: Pattern<'a
RecordDestructure(field_patterns) => {
RecordDestructure(desugar_record_destructures(env, scope, field_patterns))
}
RequiredField(name, field_pattern) => {
RequiredField(name, desugar_loc_pattern(env, scope, field_pattern))
ExprWrapped(e) => {
let loc_expr = Loc::at(Region::zero(), e);
let Loc { value, .. } = desugar_expr(env, scope, env.arena.alloc(loc_expr));
ExprWrapped(*value)
}
OptionalField(name, expr) => OptionalField(name, desugar_expr(env, scope, expr)),
Tuple(patterns) => {
let mut allocated = Vec::with_capacity_in(patterns.len(), env.arena);
for pattern in patterns.iter() {
Expand Down Expand Up @@ -1413,21 +1414,70 @@ fn desugar_pattern<'a>(env: &mut Env<'a>, scope: &mut Scope, pattern: Pattern<'a
}
}

fn desugar_assigned_field<'a>(
env: &mut Env<'a>,
scope: &mut Scope,
assigned_field: &Loc<AssignedField<'a, Pattern<'a>>>,
) -> Loc<AssignedField<'a, Pattern<'a>>> {
use AssignedField::*;
match assigned_field.value {
RequiredValue(name, spaces, field_pattern) => {
let value = desugar_pattern(env, scope, field_pattern.value);
Loc {
value: RequiredValue(
name,
spaces,
env.arena.alloc(Loc {
region: field_pattern.region,
value,
}),
),
region: assigned_field.region,
}
}
OptionalValue(name, spaces, field_pattern) => {
let value = desugar_pattern(env, scope, field_pattern.value);
Loc {
value: OptionalValue(
name,
spaces,
env.arena.alloc(Loc {
region: field_pattern.region,
value,
}),
),
region: assigned_field.region,
}
}
LabelOnly(..) | IgnoredValue(..) => {
*assigned_field
}
SpaceAfter(af, spaces) => {
Loc {
value: SpaceAfter(env.arena.alloc(desugar_assigned_field(env, scope, &Loc::at(Region::zero(), *af)).value), spaces),
region: assigned_field.region,
}
}
SpaceBefore(af, spaces) => {
Loc {
value: SpaceBefore(env.arena.alloc(desugar_assigned_field(env, scope, &Loc::at(Region::zero(), *af)).value), spaces),
region: assigned_field.region,
}
}
}
}

pub fn desugar_record_destructures<'a>(
env: &mut Env<'a>,
scope: &mut Scope,
field_patterns: Collection<'a, Loc<Pattern<'a>>>,
) -> Collection<'a, Loc<Pattern<'a>>> {
let mut allocated = Vec::with_capacity_in(field_patterns.len(), env.arena);
for field_pattern in field_patterns.iter() {
let value = desugar_pattern(env, scope, field_pattern.value);
allocated.push(Loc {
value,
region: field_pattern.region,
});
assigned_fields: Collection<'a, Loc<AssignedField<'a, Pattern<'a>>>>,
) -> Collection<'a, Loc<AssignedField<'a, Pattern<'a>>>> {
let mut allocated = Vec::with_capacity_in(assigned_fields.len(), env.arena);
for assigned_field in assigned_fields.iter() {
allocated.push(desugar_assigned_field(env, scope, assigned_field));
}

field_patterns.replace_items(allocated.into_bump_slice())
assigned_fields.replace_items(allocated.into_bump_slice())
}

/// Desugars a `dbg expr` expression into a statement block that prints and returns the
Expand Down
95 changes: 64 additions & 31 deletions crates/compiler/can/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,16 @@ pub fn canonicalize_pattern<'a>(
})
}

RequiredField(_name, _loc_pattern) => {
unreachable!("should have been handled in RecordDestructure");
}
OptionalField(_name, _loc_pattern) => {
unreachable!("should have been handled in RecordDestructure");
ExprWrapped(e) => {
unreachable!(
"What the hell is this? {e:#?}\nBacktrace: {:?}",
std::backtrace::Backtrace::capture()
);
// let (can_default, expr_output) = canonicalize_expr(env, var_store, scope, region, e);
// output.union(expr_output);
// Pattern::RecordDestruct {
// can_default
// }
}

List(patterns) => {
Expand Down Expand Up @@ -846,27 +851,27 @@ pub fn canonicalize_record_destructs<'a>(
scope: &mut Scope,
output: &mut Output,
pattern_type: PatternType,
patterns: &ast::Collection<Loc<ast::Pattern<'a>>>,
assigned_fields: &ast::Collection<Loc<ast::AssignedField<'a, ast::Pattern<'a>>>>,
region: Region,
permit_shadows: PermitShadows,
) -> (Vec<Loc<RecordDestruct>>, Option<Pattern>) {
use ast::Pattern::*;
use ast::AssignedField::*;

let mut destructs = Vec::with_capacity(patterns.len());
let mut destructs = Vec::with_capacity(assigned_fields.len());
let mut opt_erroneous = None;

for loc_pattern in patterns.iter() {
match loc_pattern.value.extract_spaces().item {
Identifier { ident: label } => {
match scope.introduce(label.into(), region) {
for loc_assigned_field in assigned_fields.iter() {
match loc_assigned_field.value.extract_spaces().item {
LabelOnly(label) => {
match scope.introduce(label.value.into(), region) {
Ok(symbol) => {
output.references.insert_bound(symbol);

destructs.push(Loc {
region: loc_pattern.region,
region: loc_assigned_field.region,
value: RecordDestruct {
var: var_store.fresh(),
label: Lowercase::from(label),
label: Lowercase::from(label.value),
symbol,
typ: DestructType::Required,
},
Expand All @@ -892,9 +897,10 @@ pub fn canonicalize_record_destructs<'a>(
};
}

RequiredField(label, loc_guard) => {
RequiredValue(label, _, loc_guard) => {
// a guard does not introduce the label into scope!
let symbol = scope.scopeless_symbol(&Ident::from(label), loc_pattern.region);
let symbol =
scope.scopeless_symbol(&Ident::from(label.value), loc_assigned_field.region);
let can_guard = canonicalize_pattern(
env,
var_store,
Expand All @@ -907,37 +913,39 @@ pub fn canonicalize_record_destructs<'a>(
);

destructs.push(Loc {
region: loc_pattern.region,
region: loc_assigned_field.region,
value: RecordDestruct {
var: var_store.fresh(),
label: Lowercase::from(label),
label: Lowercase::from(label.value),
symbol,
typ: DestructType::Guard(var_store.fresh(), can_guard),
},
});
}
OptionalField(label, loc_default) => {
OptionalValue(
label,
_,
Loc {
value: ast::Pattern::ExprWrapped(default),
region: loc_region,
},
) => {
// an optional DOES introduce the label into scope!
match scope.introduce(label.into(), region) {
match scope.introduce(label.value.into(), region) {
Ok(symbol) => {
let (can_default, expr_output) = canonicalize_expr(
env,
var_store,
scope,
loc_default.region,
&loc_default.value,
);
let (can_default, expr_output) =
canonicalize_expr(env, var_store, scope, *loc_region, default);

// an optional field binds the symbol!
output.references.insert_bound(symbol);

output.union(expr_output);

destructs.push(Loc {
region: loc_pattern.region,
region: loc_assigned_field.region,
value: RecordDestruct {
var: var_store.fresh(),
label: Lowercase::from(label),
label: Lowercase::from(label.value),
symbol,
typ: DestructType::Optional(var_store.fresh(), can_default),
},
Expand All @@ -962,9 +970,34 @@ pub fn canonicalize_record_destructs<'a>(
}
};
}
OptionalValue(label, _, loc_guard) => {
// a guard does not introduce the label into scope!
let symbol =
scope.scopeless_symbol(&Ident::from(label.value), loc_assigned_field.region);
let can_guard = canonicalize_pattern(
env,
var_store,
scope,
output,
pattern_type,
&loc_guard.value,
loc_guard.region,
permit_shadows,
);

destructs.push(Loc {
region: loc_assigned_field.region,
value: RecordDestruct {
var: var_store.fresh(),
label: Lowercase::from(label.value),
symbol,
typ: DestructType::Guard(var_store.fresh(), can_guard),
},
});
}
_ => unreachable!(
"Any other pattern should have given a parse error: {:?}",
loc_pattern.value
"Any other pattern should have given a parse error: {:#?}",
loc_assigned_field.value
),
}
}
Expand Down
Loading