From 7ec3af9efca26289cb0a5c3ba34f78ca9b1dabf5 Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Tue, 15 Oct 2024 17:21:48 +0200 Subject: [PATCH] Fixed resolution of enum names --- sway-core/src/language/call_path.rs | 142 +++++++++--------- .../ast_node/expression/typed_expression.rs | 20 ++- .../namespace/lexical_scope.rs | 12 +- .../semantic_analysis/namespace/namespace.rs | 6 +- .../src/semantic_analysis/namespace/root.rs | 10 +- .../semantic_analysis/namespace/trait_map.rs | 6 +- 6 files changed, 112 insertions(+), 84 deletions(-) diff --git a/sway-core/src/language/call_path.rs b/sway-core/src/language/call_path.rs index 33e44c93a24..ab00ad21b45 100644 --- a/sway-core/src/language/call_path.rs +++ b/sway-core/src/language/call_path.rs @@ -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 => { @@ -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 = 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 = 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 diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index 15339be840e..88fde0600b4 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -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, @@ -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(), @@ -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(), diff --git a/sway-core/src/semantic_analysis/namespace/lexical_scope.rs b/sway-core/src/semantic_analysis/namespace/lexical_scope.rs index 0a85f6a3f4c..04b75409c49 100644 --- a/sway-core/src/semantic_analysis/namespace/lexical_scope.rs +++ b/sway-core/src/semantic_analysis/namespace/lexical_scope.rs @@ -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( @@ -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) { diff --git a/sway-core/src/semantic_analysis/namespace/namespace.rs b/sway-core/src/semantic_analysis/namespace/namespace.rs index 60d1c9d4a39..637617af014 100644 --- a/sway-core/src/semantic_analysis/namespace/namespace.rs +++ b/sway-core/src/semantic_analysis/namespace/namespace.rs @@ -306,7 +306,11 @@ impl Namespace { self_type: Option, ) -> Result { 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); diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 0f3409f0396..8f33ab5a29a 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -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) => { @@ -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)) => { @@ -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(), diff --git a/sway-core/src/semantic_analysis/namespace/trait_map.rs b/sway-core/src/semantic_analysis/namespace/trait_map.rs index 8a5ac48d123..3468492d067 100644 --- a/sway-core/src/semantic_analysis/namespace/trait_map.rs +++ b/sway-core/src/semantic_analysis/namespace/trait_map.rs @@ -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()), } }