Skip to content

Commit

Permalink
Fixed resolution of enum names
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcnn committed Oct 15, 2024
1 parent eb34879 commit 7ec3af9
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 84 deletions.
142 changes: 71 additions & 71 deletions sway-core/src/language/call_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ impl CallPath {
///
/// Paths to _external_ libraries such `std::lib1::lib2::my_obj` are considered full already
/// and are left unchanged since `std` is a root of the package `std`.
pub fn to_fullpath(&self, engines: &Engines, namespace: &Namespace) -> CallPath {
pub fn to_fullpath(&self, _engines: &Engines, namespace: &Namespace) -> CallPath {
match self.callpath_type {
CallPathType::Resolved => self.clone(),
CallPathType::RelativeToPackageRoot => {
Expand All @@ -389,82 +389,82 @@ impl CallPath {
// let problem = self.suffix.as_str() == "Ord" && matches!(self.callpath_type, CallPathType::Ambiguous);

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.
// let mut synonym_prefixes = vec![];
// let mut is_external = false;
// let mut is_absolute = false;

if let Some(mod_path) = namespace.current_module().read(engines, |m| {
if m.current_items().symbols().contains_key(&self.suffix) {
// if problem { dbg!("In symbols"); };
None
} else if let Some((_, path, _, _)) = m
.current_items()
.use_item_synonyms
.get(&self.suffix)
.cloned()
{
// if problem { dbg!("In item synonyms"); };
Some(path)
} else if let Some(paths_and_decls) = m
.current_items()
.use_glob_synonyms
.get(&self.suffix)
.cloned()
{
// if problem { dbg!("In glob_synonyms"); };
if paths_and_decls.len() == 1 {
Some(paths_and_decls[0].0.clone())
} else {
None
}
} else {
// if problem { dbg!("Not bound"); };
None
}
}) {
// if problem { dbg!(&mod_path); };
CallPath {
prefixes: mod_path.clone(),
suffix: self.suffix.clone(),
callpath_type: CallPathType::Resolved,
}
// synonym_prefixes.clone_from(&mod_path);
// is_absolute = true;
// is_external = namespace.module_is_external(&mod_path);
}
else {
// // 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.
// // let mut synonym_prefixes = vec![];
// // let mut is_external = false;
// // let mut is_absolute = false;
//
// if let Some(mod_path) = namespace.current_module().read(engines, |m| {
// if m.current_items().symbols().contains_key(&self.suffix) {
// // if problem { dbg!("In symbols"); };
// None
// } else if let Some((_, path, _, _)) = m
// .current_items()
// .use_item_synonyms
// .get(&self.suffix)
// .cloned()
// {
// // if problem { dbg!("In item synonyms"); };
// Some(path)
// } else if let Some(paths_and_decls) = m
// .current_items()
// .use_glob_synonyms
// .get(&self.suffix)
// .cloned()
// {
// // if problem { dbg!("In glob_synonyms"); };
// if paths_and_decls.len() == 1 {
// Some(paths_and_decls[0].0.clone())
// } else {
// None
// }
// } else {
// // if problem { dbg!("Not bound"); };
// None
// }
// }) {
// // if problem { dbg!(&mod_path); };
// CallPath {
// prefixes: mod_path.clone(),
// suffix: self.suffix.clone(),
// callpath_type: CallPathType::Resolved,
// }
// // synonym_prefixes.clone_from(&mod_path);
// // is_absolute = true;
// // is_external = namespace.module_is_external(&mod_path);
// }
// else {
CallPath {
prefixes: namespace.current_mod_path.clone(),
suffix: self.suffix.clone(),
callpath_type: CallPathType::Resolved,
}

// let mut prefixes: Vec<Ident> = vec![];
//
// if !is_external {
// prefixes.push(namespace.current_package_name().clone());
// if problem { dbg!(&prefixes); };

// if !is_absolute {
// for mod_path in namespace.current_mod_path() {
// prefixes.push(mod_path.clone());
// }
// }
// if problem { dbg!(&prefixes); };
}

// prefixes.extend(synonym_prefixes);

// if problem { dbg!(&prefixes); };
//
// CallPath {
// prefixes,
// suffix: self.suffix.clone(),
// callpath_type: CallPathType::Resolved,
// }
// // let mut prefixes: Vec<Ident> = vec![];
// //
// // if !is_external {
// // prefixes.push(namespace.current_package_name().clone());
// // if problem { dbg!(&prefixes); };
//
// // if !is_absolute {
// // for mod_path in namespace.current_mod_path() {
// // prefixes.push(mod_path.clone());
// // }
// // }
// // if problem { dbg!(&prefixes); };
// }
//
// // prefixes.extend(synonym_prefixes);
//
// // if problem { dbg!(&prefixes); };
// //
// // CallPath {
// // prefixes,
// // suffix: self.suffix.clone(),
// // callpath_type: CallPathType::Resolved,
// // }
} else if namespace.current_module_has_submodule(&self.prefixes[0])
{
// Qualified path relative to the current module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,16 @@ impl ty::TyExpression {
prefixes: vec![],
suffix: name.clone(),
callpath_type: CallPathType::Ambiguous,
}.to_fullpath(engines, ctx.namespace());
};//.to_fullpath(engines, ctx.namespace());

// let problem =
// name.as_str() == "None"
// && ctx.namespace().current_mod_path().len() == 2
// && ctx.namespace().current_mod_path()[0].as_str() == "std"
// && ctx.namespace().current_mod_path()[1].as_str() == "vec";
// if problem {
// dbg!(&call_path);
// };

Self::type_check_delineated_path(
handler,
Expand Down Expand Up @@ -1408,6 +1417,14 @@ impl ty::TyExpression {
suffix,
callpath_type,
};
// let mod_path = ctx.namespace.current_mod_path();
// let problem = call_path.suffix.as_str() == "Some"
// && mod_path.len() == 2
// && mod_path[0].as_str() == "std"
// && mod_path[1].as_str() == "vec";
// if problem {
// dbg!(&call_path);
// }
if matches!(
ctx.namespace().resolve_call_path_typed(
&Handler::default(),
Expand Down Expand Up @@ -1700,6 +1717,7 @@ impl ty::TyExpression {
instantiate_constant_expression(ctx, const_ref, call_path_binding)
}
(false, None, None, None) => {
//dbg!("typecheck delineated path");
return Err(handler.emit_err(CompileError::SymbolNotFound {
name: unknown_call_path_binding.inner.call_path.suffix.clone(),
span: unknown_call_path_binding.inner.call_path.suffix.span(),
Expand Down
12 changes: 8 additions & 4 deletions sway-core/src/semantic_analysis/namespace/lexical_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,12 @@ impl Items {
self.symbols
.get(name)
.cloned()
.ok_or_else(|| CompileError::SymbolNotFound {
.ok_or_else(|| {
// dbg!("check_symbol");
CompileError::SymbolNotFound {
name: name.clone(),
span: name.span(),
})
}})
}

pub(crate) fn check_symbols_unique_while_collecting_unifications(
Expand All @@ -712,10 +714,12 @@ impl Items {
.read()
.get(&name.into())
.cloned()
.ok_or_else(|| CompileError::SymbolNotFound {
.ok_or_else(|| {
// dbg!("check_symbols_unique_while_collecting_unifications");
CompileError::SymbolNotFound {
name: name.clone(),
span: name.span(),
})
}})
}

pub(crate) fn clear_symbols_unique_while_collecting_unifications(&self) {
Expand Down
6 changes: 5 additions & 1 deletion sway-core/src/semantic_analysis/namespace/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ impl Namespace {
self_type: Option<TypeId>,
) -> Result<ResolvedDeclaration, ErrorEmitted> {
let full_path = call_path.to_fullpath(engines, &self);
// let problem = true; //call_path.suffix.as_str() == "Ord";
// let problem =
// call_path.suffix.as_str() == "Some"
// && self.current_mod_path.len() == 2
// && self.current_mod_path[0].as_str() == "std"
// && self.current_mod_path[1].as_str() == "vec";
// if problem {
// dbg!(call_path);
// dbg!(&full_path);
Expand Down
10 changes: 5 additions & 5 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ impl Root {
let decl_engine = engines.de();
let parsed_decl_engine = engines.pe();

let (decl, mut path) = self.item_lookup(handler, engines, enum_name, src, dst)?;
path.push(enum_name.clone());
let (decl, /*mut*/ path) = self.item_lookup(handler, engines, enum_name, src, dst)?;
//path.push(enum_name.clone());

match decl {
ResolvedDeclaration::Parsed(decl) => {
Expand Down Expand Up @@ -741,8 +741,8 @@ impl Root {
let parsed_decl_engine = engines.pe();
let decl_engine = engines.de();

let (decl, mut path) = self.item_lookup(handler, engines, enum_name, src, dst)?;
path.push(enum_name.clone());
let (decl, /*mut*/ path) = self.item_lookup(handler, engines, enum_name, src, dst)?;
// path.push(enum_name.clone());

match decl {
ResolvedDeclaration::Parsed(Declaration::EnumDeclaration(decl_id)) => {
Expand Down Expand Up @@ -1236,7 +1236,7 @@ impl Root {
}
}
// Symbol not found
// println!("resolve symbol helper");
// dbg!("resolve symbol helper");
Err(handler.emit_err(CompileError::SymbolNotFound {
name: symbol.clone(),
span: symbol.span(),
Expand Down
6 changes: 4 additions & 2 deletions sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,10 +1318,12 @@ impl TraitMap {
span: symbol.span(),
},
)),
Ordering::Less => Err(handler.emit_err(CompileError::SymbolNotFound {
Ordering::Less => {
// if symbol.as_str() == "Some" { panic!(); };
Err(handler.emit_err(CompileError::SymbolNotFound {
name: symbol.clone(),
span: symbol.span(),
})),
}))},
Ordering::Equal => Ok(candidates.values().next().unwrap().clone()),
}
}
Expand Down

0 comments on commit 7ec3af9

Please sign in to comment.