Skip to content

Commit

Permalink
Merge d507b84 into e1fcf98
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcnn authored Jun 5, 2024
2 parents e1fcf98 + d507b84 commit 85fd359
Show file tree
Hide file tree
Showing 21 changed files with 391 additions and 34 deletions.
14 changes: 10 additions & 4 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,7 @@ pub fn dependency_namespace(
};

root_module.write(engines, |root_module| {
root_module.is_external = true;
// root_module.is_external = true;
root_module.name.clone_from(&name);
root_module.visibility = Visibility::Public;
});
Expand All @@ -1606,7 +1606,7 @@ pub fn dependency_namespace(
let dep_node = edge.target();
let dep_name = kebab_to_snake_case(&edge.weight().name);
let dep_edge = edge.weight();
let dep_namespace = match dep_edge.kind {
let mut dep_namespace = match dep_edge.kind {
DepKind::Library => lib_namespace_map
.get(&dep_node)
.cloned()
Expand All @@ -1628,12 +1628,12 @@ pub fn dependency_namespace(
contract_id_value,
experimental,
)?;
module.is_external = true;
module.name = name;
module.visibility = Visibility::Public;
module
}
};
dep_namespace.is_external = true;
root_module.insert_submodule(dep_name, dep_namespace);
let dep = &graph[dep_node];
if dep.name == CORE {
Expand All @@ -1648,7 +1648,7 @@ pub fn dependency_namespace(
root_module.insert_submodule(CORE.to_string(), core_namespace.clone());
}
}

let mut root = namespace::Root::from(root_module);

let _ = root.star_import_with_reexports(
Expand Down Expand Up @@ -2350,6 +2350,8 @@ pub fn build(
Err(errs) => return fail(&[], &errs),
};

// dep_namespace.set_external(false);

let compiled_without_tests = compile(
&descriptor,
&profile,
Expand Down Expand Up @@ -2423,6 +2425,8 @@ pub fn build(
}
};

// dep_namespace.set_external(false);

let mut compiled = compile(
&descriptor,
&profile,
Expand Down Expand Up @@ -2647,6 +2651,8 @@ pub fn check(
)
.expect("failed to create dependency namespace");

// dep_namespace.set_external(false);

let profile = BuildProfile {
terse: terse_mode,
..BuildProfile::debug()
Expand Down
38 changes: 36 additions & 2 deletions sway-core/src/language/call_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ impl CallPath {
.collect::<Vec<_>>()
}

pub fn ident_to_fullpath(suffix: Ident, namespace: &Namespace) -> CallPath {
let mut res : Self = suffix.clone().into();
if let Some(ref pkg_name) = namespace.root_module().name {
res.prefixes.push(pkg_name.clone())
};
for mod_path in namespace.mod_path() {
res.prefixes.push(mod_path.clone())
};
res.is_absolute = true;
res
}

/// Convert a given [CallPath] to a symbol to a full [CallPath] from the root of the project
/// in which the symbol is declared. For example, given a path `pkga::SOME_CONST` where `pkga`
/// is an _internal_ library of a package named `my_project`, the corresponding call path is
Expand All @@ -341,7 +353,12 @@ impl CallPath {
return self.clone();
}

if self.prefixes.is_empty() {
// let problem = self.suffix.as_str() == "AbiEncode";
// if problem {
// println!("namespace mod_path: \"{}\"", namespace.mod_path.iter().map(|x| x.as_str()).collect::<Vec<_>>().join("::"));
// }

if self.prefixes.is_empty() {
// Given a path to a symbol that has no prefixes, discover the path to the symbol as a
// combination of the package name in which the symbol is defined and the path to the
// current submodule.
Expand All @@ -350,12 +367,17 @@ impl CallPath {
let mut is_absolute = false;

if let Some(mod_path) = namespace.program_id(engines).read(engines, |m| {
if let Some((_, path, _)) = m
if m.current_items().symbols().contains_key(&self.suffix) {
// if problem { println!("No prefix, local binding"); };
None
}
else if let Some((_, path, _)) = m
.current_items()
.use_item_synonyms
.get(&self.suffix)
.cloned()
{
// if problem { println!("No prefix, item import, path: {}", path.iter().map(|x| x.as_str()).collect::<Vec<_>>().join("::")); };
Some(path)
} else if let Some(paths_and_decls) = m
.current_items()
Expand All @@ -364,11 +386,14 @@ impl CallPath {
.cloned()
{
if paths_and_decls.len() == 1 {
// if problem { println!("No prefix, glob import, path: {}", paths_and_decls[0].0.iter().map(|x| x.as_str()).collect::<Vec<_>>().join("::")); };
Some(paths_and_decls[0].0.clone())
} else {
// if problem { println!("No prefix, non-one glob imports"); };
None
}
} else {
// if problem { println!("No prefix, no binding"); };
None
}
}) {
Expand All @@ -379,7 +404,12 @@ impl CallPath {
.submodule(engines, &[mod_path[0].clone()]);
if let Some(submodule) = submodule {
is_external = submodule.read(engines, |m| m.is_external);
// println!("Found submodule. is_external = {}", is_external);
}
// else {
// println!("No submodule found.");
// }
// if problem { println!("is_external: {}", is_external); };
}

let mut prefixes: Vec<Ident> = vec![];
Expand All @@ -398,6 +428,10 @@ impl CallPath {

prefixes.extend(synonym_prefixes);

// if problem {
// println!("final prefix: {}", prefixes.iter().map(|x| x.as_str()).collect::<Vec<_>>().join("::"));
// };

CallPath {
prefixes,
suffix: self.suffix.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl ty::TyAbiDecl {
// from the same ABI later, during method application typechecking.
let _ = ctx.insert_trait_implementation(
&Handler::default(),
CallPath::from(self.name.clone()),
CallPath::ident_to_fullpath(self.name.clone(), ctx.namespace()),
vec![],
type_id,
&all_items,
Expand Down
5 changes: 3 additions & 2 deletions sway-core/src/semantic_analysis/ast_node/declaration/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ impl ty::TyEnumDecl {
);
}

let mut call_path: CallPath = name.into();
call_path = call_path.to_fullpath(ctx.engines(), ctx.namespace());
let call_path = CallPath::ident_to_fullpath(name, ctx.namespace());
// let mut call_path: CallPath = name.into();
// call_path = call_path.to_fullpath(ctx.engines(), ctx.namespace());

// create the enum decl
let decl = ty::TyEnumDecl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,14 @@ impl TyImplTrait {
let self_type_id = self_type_param.type_id;

// create the trait name
let trait_name = CallPath {
prefixes: vec![],
suffix: match &&*type_engine.get(implementing_for.type_id) {
let suffix = match &&*type_engine.get(implementing_for.type_id) {
TypeInfo::Custom {
qualified_call_path: call_path,
..
} => call_path.call_path.suffix.clone(),
_ => Ident::new_with_override("r#Self".into(), implementing_for.span()),
},
is_absolute: false,
};
let trait_name = CallPath::ident_to_fullpath(suffix, ctx.namespace());

// Type check the type parameters.
let new_impl_type_parameters = TypeParameter::type_check_type_params(
Expand Down
26 changes: 24 additions & 2 deletions sway-core/src/semantic_analysis/ast_node/declaration/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,31 @@ impl ty::TyStructDecl {
new_fields.push(ty::TyStructField::type_check(handler, ctx.by_ref(), field)?);
}

let mut path: CallPath = name.into();
path = path.to_fullpath(ctx.engines(), ctx.namespace());



// let mut path: CallPath = name.clone().into();
// //path = path.to_fullpath(ctx.engines, ctx.namespace());
// // if !ctx.namespace().module(ctx.engines).is_external {
// if let Some(ref pkg_name) = ctx.namespace().root_module().name {
// path.prefixes.push(pkg_name.clone());
// };
// // };
// for mod_path in ctx.namespace().mod_path() {
// path.prefixes.push(mod_path.clone())
// };
// path.is_absolute = true;
//
// // if name.as_str() == "BufferReader"
// // || name.as_str() == "TestStruct"
// // {
// // dbg!(&name);
// // dbg!(&path.prefixes);
// // dbg!(&span);
// // };

let path = CallPath::ident_to_fullpath(name, ctx.namespace());

// create the struct decl
let decl = ty::TyStructDecl {
call_path: path,
Expand Down
12 changes: 2 additions & 10 deletions sway-core/src/semantic_analysis/ast_node/declaration/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ impl TyTraitDecl {
// to allow methods to use those functions
ctx.insert_trait_implementation(
handler,
CallPath {
prefixes: vec![],
suffix: name.clone(),
is_absolute: false,
},
CallPath::ident_to_fullpath(name.clone(), ctx.namespace),
new_type_parameters.iter().map(|x| x.into()).collect(),
self_type,
&dummy_interface_surface,
Expand Down Expand Up @@ -180,11 +176,7 @@ impl TyTraitDecl {
// to allow methods to use those functions
ctx.insert_trait_implementation(
handler,
CallPath {
prefixes: vec![],
suffix: name.clone(),
is_absolute: false,
},
CallPath::ident_to_fullpath(name.clone(), ctx.namespace()),
new_type_parameters.iter().map(|x| x.into()).collect(),
self_type,
&dummy_interface_surface,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ pub(crate) fn instantiate_struct_field_access(

let field = match decl.find_field(&field_to_access) {
Some(field) => {
// if decl.call_path.suffix.as_str() == "TestStruct" {
// dbg!(&field.name);
// dbg!(&decl.call_path.prefixes);
// for field in decl.fields.iter() {
// dbg!(&field.name);
// }
// dbg!(&decl.span.as_str());
// };
//
if is_public_struct_access && field.is_private() {
return Err(handler.emit_err(CompileError::StructFieldIsPrivate {
field_name: (&field_to_access).into(),
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/semantic_analysis/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ impl ty::TyModule {
.iter()
.find(|(submod_name, _submodule)| eval_mod_name == submod_name)
.unwrap();
// println!("Typechecking submodule {}", name.as_str());
Ok((
name.clone(),
ty::TySubmodule::type_check(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ fn default_with_contract_id_inner(
let mut ns = Namespace::init_root(root);
// This is pretty hacky but that's okay because of this code is being removed pretty soon
ns.root.module.name = ns_name;
ns.root.module.is_external = true;
ns.root.module.visibility = Visibility::Public;
let type_check_ctx = TypeCheckContext::from_namespace(&mut ns, engines, experimental);
let typed_node = TyAstNode::type_check(handler, type_check_ctx, &ast_node).unwrap();
Expand Down
Loading

0 comments on commit 85fd359

Please sign in to comment.