Skip to content

Commit

Permalink
comment
Browse files Browse the repository at this point in the history
  • Loading branch information
rahxephon89 committed Jul 9, 2024
1 parent b4af060 commit ce9a116
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Diagnostics:
error: resource indexing can only applied to a resource type
error: resource indexing can only applied to a resource type (a struct type which has key ability)
┌─ tests/ability-check/index_ability_err_2.move:9:17
9 │ let _ = X<bool>[addr];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

Diagnostics:
error: indexing is only allowed for resource or vector
error: indexing can only be applied to a vector or a resource type (a struct type which has key ability)
┌─ tests/checking/typing/index_err.move:14:9
14 │ UNUSED_Test[@0x1];
│ ^^^^^^^^^^^^^^^^^

error: indexing is only allowed for resource or vector
error: indexing can only be applied to a vector or a resource type (a struct type which has key ability)
┌─ tests/checking/typing/index_err.move:19:9
19 │ &test::UNUSED_Test[@0x1];
Expand Down
2 changes: 1 addition & 1 deletion third_party/move/move-compiler/src/expansion/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ pub enum Exp_ {

Borrow(bool, Box<Exp>),
ExpDotted(Box<ExpDotted>),
Index(Box<Exp>, Box<Exp>), // spec only for language version V1
Index(Box<Exp>, Box<Exp>), // spec only unless language >= v2

Cast(Box<Exp>, Type),
Annotate(Box<Exp>, Type),
Expand Down
70 changes: 39 additions & 31 deletions third_party/move/move-model/src/builder/exp_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
ReceiverFunctionInstance, ReferenceKind, Substitution, Type, TypeDisplayContext,
TypeUnificationError, UnificationContext, Variance, WideningOrder, BOOL_TYPE,
},
FunId,
};
use codespan_reporting::diagnostic::Severity;
use itertools::Itertools;
Expand Down Expand Up @@ -3312,6 +3313,27 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo
}
}

//TODO: make a Lazy const for mid and fid of vector functions
fn get_vector_borrow(&self, mutable: bool) -> (Option<ModuleId>, Option<FunId>) {
let target_str = if mutable {
"vector::borrow_mut"
} else {
"vector::borrow"
};
for m in self.env().get_modules() {
if m.is_std_vector() {
let mid = m.get_id();
for f in m.get_functions() {
if f.get_full_name_str() == target_str {
let fid = f.get_id();
return (Some(mid), Some(fid));
}
}
}
}
(None, None)
}

fn call_to_vector_borrow_for_index_op(
&mut self,
loc: &Loc,
Expand Down Expand Up @@ -3343,35 +3365,21 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo
if self.had_errors {
return self.new_error_exp();
}
let target_str = if mutable {
"vector::borrow_mut"
} else {
"vector::borrow"
};
// TODO: comment out because of #13888
for m in self.env().get_modules() {
if m.is_std_vector() {
let mid = m.get_id();
for f in m.get_functions() {
if f.get_full_name_str() == target_str {
let fid = f.get_id();
let instantiated_inner = self.subs.specialize(&inner_ty);
let node_id = self.env().new_node(
loc.clone(),
Type::Reference(
ReferenceKind::from_is_mut(mutable),
Box::new(instantiated_inner.clone()),
),
);
self.set_node_instantiation(node_id, vec![inner_ty.clone()]);
let call = ExpData::Call(node_id, Operation::MoveFunction(mid, fid), vec![
vec_exp_e.into_exp(),
idx_exp_e.clone().into_exp(),
]);
return call;
}
}
}
if let (Some(mid), Some(fid)) = self.get_vector_borrow(mutable) {
let instantiated_inner = self.subs.specialize(&inner_ty);
let node_id = self.env().new_node(
loc.clone(),
Type::Reference(
ReferenceKind::from_is_mut(mutable),
Box::new(instantiated_inner.clone()),
),
);
self.set_node_instantiation(node_id, vec![inner_ty.clone()]);
let call = ExpData::Call(node_id, Operation::MoveFunction(mid, fid), vec![
vec_exp_e.into_exp(),
idx_exp_e.clone().into_exp(),
]);
return call;
}
ExpData::Invalid(self.env().new_node_id())
}
Expand Down Expand Up @@ -3413,7 +3421,7 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo
loc, target, index, mutable, ty, context,
));
} else {
self.error(loc, "resource indexing can only applied to a resource type");
self.error(loc, "resource indexing can only applied to a resource type (a struct type which has key ability)");
call = Some(self.new_error_exp());
}
} else if self
Expand All @@ -3426,7 +3434,7 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo
.language_version
.is_at_least(LanguageVersion::V2_0)
{
self.error(loc, "indexing is only allowed for resource or vector");
self.error(loc, "indexing can only be applied to a vector or a resource type (a struct type which has key ability)");
call = Some(self.new_error_exp());
}
}
Expand Down

0 comments on commit ce9a116

Please sign in to comment.