Skip to content

Commit

Permalink
[move 2024][alpha] Type inference holes (#17059)
Browse files Browse the repository at this point in the history
## Description 

- Added placeholders for type inference `_`

## Test Plan 

- Added tests

---
If your changes are not user-facing and do not break anything, you can
skip the following section. Otherwise, please briefly describe what has
changed under the Release Notes section.

### Type of Change (Check all that apply)

- [ ] protocol change
- [X] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes

Move 2024.alpha now supports placeholders for type inference `_`

---------

Co-authored-by: Todd Nowacki <tmn@calibra.com>
Co-authored-by: Cam Swords <cameronswords@gmail.com>
  • Loading branch information
3 people authored Apr 10, 2024
1 parent c704693 commit 70536f6
Show file tree
Hide file tree
Showing 40 changed files with 850 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ codes!(
InvalidTypeParameter: { msg: "invalid type parameter", severity: NonblockingError },
InvalidPattern: { msg: "invalid pattern", severity: BlockingError },
UnboundVariant: { msg: "unbound variant", severity: BlockingError },
InvalidTypeAnnotation: { msg: "invalid type annotation", severity: NonblockingError },
],
// errors for typing rules. mostly typing/translate
TypeSafety: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub enum FeatureGate {
AutoborrowEq,
CleverAssertions,
NoParensCast,
TypeHoles,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug, PartialOrd, Ord, Default)]
Expand Down Expand Up @@ -131,7 +132,7 @@ pub fn valid_editions_for_feature(feature: FeatureGate) -> Vec<Edition> {
static SUPPORTED_FEATURES: Lazy<BTreeMap<Edition, BTreeSet<FeatureGate>>> =
Lazy::new(|| BTreeMap::from_iter(Edition::ALL.iter().map(|e| (*e, e.features()))));

const E2024_ALPHA_FEATURES: &[FeatureGate] = &[FeatureGate::MacroFuns];
const E2024_ALPHA_FEATURES: &[FeatureGate] = &[FeatureGate::MacroFuns, FeatureGate::TypeHoles];

const E2024_BETA_FEATURES: &[FeatureGate] = &[
FeatureGate::NestedUse,
Expand Down Expand Up @@ -271,6 +272,7 @@ impl FeatureGate {
FeatureGate::AutoborrowEq => "Automatic borrowing is",
FeatureGate::CleverAssertions => "Clever `assert!`, `abort`, and `#[error]` are",
FeatureGate::NoParensCast => "'as' without parentheses is",
FeatureGate::TypeHoles => "'_' placeholders for type inference are",
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3390,8 +3390,7 @@ fn lvalues(context: &mut Context, e: Box<P::Exp>) -> Option<LValue> {
}

fn assign(context: &mut Context, sp!(loc, e_): P::Exp) -> Option<E::LValue> {
use E::LValue_ as EL;
use E::ModuleAccess_ as M;
use E::{LValue_ as EL, ModuleAccess_ as M};
use P::Exp_ as PE;
match e_ {
PE::Name(name) => {
Expand Down Expand Up @@ -3765,6 +3764,13 @@ fn check_valid_type_parameter_name(
is_macro: Option<Loc>,
n: &Name,
) -> Result<(), ()> {
// TODO move these names to a more central place?
if n.value == symbol!("_") {
let diag = restricted_name_error(NameCase::TypeParameter, n.loc, "_");
context.env().add_diag(diag);
return Err(());
}

const SYNTAX_IDENTIFIER_NOTE: &str = "Type parameter names starting with '$' indicate that \
their arguments do not have to satisfy certain constraints before the macro is expanded, \
meaning types like '&mut u64' or '(bool, u8)' may be used as arguments.";
Expand All @@ -3785,6 +3791,19 @@ fn check_valid_type_parameter_name(
);
diag.add_note(SYNTAX_IDENTIFIER_NOTE);
context.env().add_diag(diag);
} else {
let next_char = n.value.chars().nth(1).unwrap();
if !next_char.is_ascii_alphabetic() {
let msg = format!(
"Invalid type parameter name '{}'. \
Following the '$', the '{} fun' type parameter must be have a valid type \
parameter name starting with a letter 'a'..'z' or 'A'..'Z'",
n, MACRO_MODIFIER
);
let mut diag = diag!(Declarations::InvalidName, (n.loc, msg));
diag.add_note(SYNTAX_IDENTIFIER_NOTE);
context.env().add_diag(diag);
}
}
} else if is_syntax_ident {
let msg = format!(
Expand All @@ -3798,11 +3817,6 @@ fn check_valid_type_parameter_name(
}

// TODO move these names to a more central place?
if n.value == symbol!("_") {
let diag = restricted_name_error(NameCase::TypeParameter, n.loc, "_");
context.env().add_diag(diag);
return Err(());
}
check_restricted_names(
context,
NameCase::TypeParameter,
Expand Down
Loading

0 comments on commit 70536f6

Please sign in to comment.