Skip to content

Commit

Permalink
revert merge fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcnn committed Jun 3, 2024
1 parent 396d338 commit f764499
Showing 1 changed file with 1 addition and 140 deletions.
141 changes: 1 addition & 140 deletions sway-core/src/semantic_analysis/namespace/lexical_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,146 +35,7 @@ impl ResolvedFunctionDecl {
}
}

/// Scoped trait maps.
///
/// These are used to distinguish between trait maps imported using star imports (`use a::*`), trait
/// maps imported using item imports (`use a::X`), and locally declared trait maps, since the latter
/// two may shadow the first.
#[derive(Clone, Debug, Default)]
pub(crate) struct ScopedTraitMaps {
pub(crate) local_decls: TraitMap,
pub(crate) item_imports: TraitMap,
pub(crate) glob_imports: TraitMap,
}

impl ScopedTraitMaps {
pub fn get_items_for_type(&self, engines: &Engines, type_id: TypeId) -> Vec<ResolvedTraitImplItem> {
let item_imports = self.item_imports.get_items_for_type(engines, type_id);
let decls_and_item_imports = self.local_decls.get_items_for_type(engines, type_id)
.extend_from_slice(item_imports.as_slice());
if !decls_and_item_imports.is_empty() {
decls_and_item_imports
}
else {
self.glob_imports.get_items_for_type(engines, type_id)
}
}

pub fn get_impl_spans_for_type(&self, engines: &Engines, type_id: &TypeId) -> Vec<Span> {
let item_imports = self.item_imports.get_impl_spans_for_type(engines, type_id);
let decls_and_item_imports = self.local_decls.get_impl_spans_for_type(engines, type_id)
.extend_from_slice(item_imports.as_slice());
if !decls_and_item_imports.is_empty() {
decls_and_item_imports
}
else {
self.glob_imports.get_impl_spans_for_type(engines, type_id)
}
}

pub fn get_impl_spans_for_trait_name(&self, engines: &Engines, trait_name: &CallPath) -> Vec<Span> {
let item_imports = self.item_imports.get_impl_spans_for_trait_name(engines, trait_name);
let decls_and_item_imports = self.local_decls.get_impl_spans_for_trait_name(engines, trait_name)
.extend_from_slice(item_imports.as_slice());
if !decls_and_item_imports.is_empty() {
decls_and_item_imports
}
else {
self.glob_imports.get_impl_spans_for_trait_name(engines, trait_name)
}
}

pub fn get_trait_item_for_type(&self, handler: &Handler, engines: &Engines, symbol: &Ident, type_id: TypeId, as_trait: Option<CallPath>) -> Result<ResolvedTraitImplItem, ErrorEmitted> {
let local_decl_candidates = self.local_decls.get_trait_item_for_type(handler, engines, symbol, type_id, as_trait);
let item_import_candidates = self.item_imports.get_trait_item_for_type(handler, engines, symbol, type_id, as_trait);
let total_local_and_item_import_candidates = local_decl_candidates.len() + item_import_candidates.len();
if total_local_and_item_import_candidates > 1 {
Err(handler.emit_err(
CompileError::MultipleApplicableItemsInScope {
item_name: symbol.as_str().to_string(),
item_kind: "item".to_string(),
type_name: engines.help_out(type_id).to_string(),
as_traits: local_decl_candidates
.keys()
.map(|k| {
k.clone()
.split("::")
.collect::<Vec<_>>()
.last()
.unwrap()
.to_string()
})
.collect::<Vec<_>>()
.extend(
item_import_candidates
.keys()
.map(|k| {
k.clone()
.split("::")
.collect::<Vec<_>>()
.last()
.unwrap()
.to_string()
})),
span: symbol.span(),
},
))
}
else if total_local_and_item_import_candidates == 1 {
if local_decl_candidates == 1 {
Ok(local_decl_candidates.values().next().unwrap().clone())
} else {
Ok(item_import_candidates.values().next().unwrap().clone())
}
}
else {
// No local or item imported candidates. Check the star imported candidates
let glob_import_candidates = self.glob_imports.get_trait_item_for_type(handler, engines, symbol, type_id, as_trait);
if glob_import_candidates.len() > 1 {
Err(handler.emit_err(
CompileError::MultipleApplicableItemsInScope {
item_name: symbol.as_str().to_string(),
item_kind: "item".to_string(),
type_name: engines.help_out(type_id).to_string(),
as_traits: glob_import_candidates
.keys()
.map(|k| {
k.clone()
.split("::")
.collect::<Vec<_>>()
.last()
.unwrap()
.to_string()
})
.collect::<Vec<_>>(),
span: symbol.span(),
},
))
}
else if glob_import_candidates.len() == 1 {
Ok(glob_import_candidates.values().next().unwrap().clone())
}
else {
// No candidates found
Err(handler.emit_err(CompileError::SymbolNotFound {
name: symbol.clone(),
span: symbol.span(),
}))
}
}
}

pub fn check_if_trait_constraints_are_satisfied_for_type(handler: &Handler, implementing_for: TypeId, trait_supertraits: &[TraitConstraint], access_span: &Span, engines: &Engines, try_inserting_trait_impl_on_failure: TryInsertingTraitImplOnFailure) -> Result<(), ErrorEmitted> {


}


// TODO: Error handling wrt. insertion should also happen here.
}

pub(super) type ParsedSymbolMap = im::OrdMap<Ident, Declaration>;
pub(super) type SymbolMap = im::OrdMap<Ident, ty::TyDecl>;
pub(super) type SymbolMap = im::OrdMap<Ident, ResolvedDeclaration>;
type SourceIdent = Ident;
pub(super) type GlobSynonyms = im::HashMap<Ident, Vec<(ModulePathBuf, ty::TyDecl)>>;
pub(super) type ItemSynonyms = im::HashMap<Ident, (Option<SourceIdent>, ModulePathBuf, ty::TyDecl)>;
Expand Down

0 comments on commit f764499

Please sign in to comment.