sway_core/semantic_analysis/namespace/lexical_scope.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
use crate::{
decl_engine::{parsed_engine::ParsedDeclEngineGet, parsed_id::ParsedDeclId, *},
engine_threading::{Engines, PartialEqWithEngines, PartialEqWithEnginesContext},
language::{
parsed::{Declaration, FunctionDeclaration},
ty::{self, StructAccessInfo, TyDecl, TyStorageDecl},
CallPath, Visibility,
},
namespace::*,
semantic_analysis::{ast_node::ConstShadowingMode, GenericShadowingMode},
type_system::*,
};
use super::{root::ResolvedDeclaration, TraitMap};
use parking_lot::RwLock;
use sway_error::{
error::{CompileError, ShadowingSource, StructFieldUsageContext},
handler::{ErrorEmitted, Handler},
};
use sway_types::{span::Span, IdentUnique, Named, Spanned};
use std::sync::Arc;
pub enum ResolvedFunctionDecl {
Parsed(ParsedDeclId<FunctionDeclaration>),
Typed(DeclRefFunction),
}
impl ResolvedFunctionDecl {
pub fn expect_typed(self) -> DeclRefFunction {
match self {
ResolvedFunctionDecl::Parsed(_) => panic!(),
ResolvedFunctionDecl::Typed(fn_ref) => fn_ref,
}
}
}
pub(super) type SymbolMap = im::OrdMap<Ident, ResolvedDeclaration>;
pub(super) type SymbolUniqueMap = im::OrdMap<IdentUnique, ResolvedDeclaration>;
type SourceIdent = Ident;
pub(super) type GlobSynonyms =
im::HashMap<Ident, Vec<(ModulePathBuf, ResolvedDeclaration, Visibility)>>;
pub(super) type ItemSynonyms = im::HashMap<
Ident,
(
Option<SourceIdent>,
ModulePathBuf,
ResolvedDeclaration,
Visibility,
),
>;
/// Represents a lexical scope integer-based identifier, which can be used to reference
/// specific a lexical scope.
pub type LexicalScopeId = usize;
/// Represents a lexical scope path, a vector of lexical scope identifiers, which specifies
/// the path from root to a specific lexical scope in the hierarchy.
pub type LexicalScopePath = Vec<LexicalScopeId>;
/// A `LexicalScope` contains a set of all items that exist within the lexical scope via declaration or
/// importing, along with all its associated hierarchical scopes.
#[derive(Clone, Debug, Default)]
pub struct LexicalScope {
/// The set of symbols, implementations, synonyms and aliases present within this scope.
pub items: Items,
/// The set of available scopes defined inside this scope's hierarchy.
pub children: Vec<LexicalScopeId>,
/// The parent scope associated with this scope. Will be None for a root scope.
pub parent: Option<LexicalScopeId>,
}
/// The set of items that exist within some lexical scope via declaration or importing.
#[derive(Clone, Debug, Default)]
pub struct Items {
/// An ordered map from `Ident`s to their associated declarations.
pub(crate) symbols: SymbolMap,
/// An ordered map from `IdentUnique`s to their associated declarations.
/// This uses an Arc<RwLock<SymbolUniqueMap>> so it is shared between all
/// Items clones. This is intended so we can keep the symbols of previous
/// lexical scopes while collecting_unifications scopes.
pub(crate) symbols_unique_while_collecting_unifications: Arc<RwLock<SymbolUniqueMap>>,
pub(crate) implemented_traits: TraitMap,
/// Contains symbols imported using star imports (`use foo::*`.).
///
/// When star importing from multiple modules the same name may be imported more than once. This
/// is not an error, but it is an error to use the name without a module path. To represent
/// this, use_glob_synonyms maps identifiers to a vector of (module path, type declaration)
/// tuples.
pub(crate) use_glob_synonyms: GlobSynonyms,
/// Contains symbols imported using item imports (`use foo::bar`).
///
/// For aliased item imports `use ::foo::bar::Baz as Wiz` the map key is `Wiz`. `Baz` is stored
/// as the optional source identifier for error reporting purposes.
pub(crate) use_item_synonyms: ItemSynonyms,
/// If there is a storage declaration (which are only valid in contracts), store it here.
pub(crate) declared_storage: Option<DeclRefStorage>,
}
impl Items {
/// Immutable access to the inner symbol map.
pub fn symbols(&self) -> &SymbolMap {
&self.symbols
}
#[allow(clippy::too_many_arguments)]
pub fn apply_storage_load(
&self,
handler: &Handler,
engines: &Engines,
namespace: &Namespace,
namespace_names: &[Ident],
fields: &[Ident],
storage_fields: &[ty::TyStorageField],
storage_keyword_span: Span,
) -> Result<(ty::TyStorageAccess, TypeId), ErrorEmitted> {
match self.declared_storage {
Some(ref decl_ref) => {
let storage = engines.de().get_storage(&decl_ref.id().clone());
storage.apply_storage_load(
handler,
engines,
namespace,
namespace_names,
fields,
storage_fields,
storage_keyword_span,
)
}
None => Err(handler.emit_err(CompileError::NoDeclaredStorage {
span: fields[0].span(),
})),
}
}
pub fn set_storage_declaration(
&mut self,
handler: &Handler,
decl_ref: DeclRefStorage,
) -> Result<(), ErrorEmitted> {
if self.declared_storage.is_some() {
return Err(handler.emit_err(CompileError::MultipleStorageDeclarations {
span: decl_ref.span(),
}));
}
self.declared_storage = Some(decl_ref);
Ok(())
}
pub fn get_all_declared_symbols(&self) -> impl Iterator<Item = &Ident> {
self.symbols().keys()
}
pub fn resolve_symbol(
&self,
handler: &Handler,
engines: &Engines,
symbol: &Ident,
) -> Result<ResolvedDeclaration, ErrorEmitted> {
// Check locally declared items. Any name clash with imports will have already been reported as an error.
if let Some(decl) = self.symbols.get(symbol) {
return Ok(decl.clone());
}
// Check item imports
if let Some((_, _, decl, _)) = self.use_item_synonyms.get(symbol) {
return Ok(decl.clone());
}
// Check glob imports
if let Some(decls) = self.use_glob_synonyms.get(symbol) {
if decls.len() == 1 {
return Ok(decls[0].1.clone());
} else if decls.is_empty() {
return Err(handler.emit_err(CompileError::Internal(
"The name {symbol} was bound in a star import, but no corresponding module paths were found",
symbol.span(),
)));
} else {
return Err(handler.emit_err(CompileError::SymbolWithMultipleBindings {
name: symbol.clone(),
paths: decls
.iter()
.map(|(path, decl, _)| get_path_for_decl(path, decl, engines))
.collect(),
span: symbol.span(),
}));
}
}
// Symbol not found
Err(handler.emit_err(CompileError::SymbolNotFound {
name: symbol.clone(),
span: symbol.span(),
}))
}
pub(crate) fn insert_parsed_symbol(
&mut self,
handler: &Handler,
engines: &Engines,
name: Ident,
item: Declaration,
const_shadowing_mode: ConstShadowingMode,
generic_shadowing_mode: GenericShadowingMode,
) -> Result<(), ErrorEmitted> {
self.insert_symbol(
handler,
engines,
name,
ResolvedDeclaration::Parsed(item),
const_shadowing_mode,
generic_shadowing_mode,
false,
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn insert_typed_symbol(
&mut self,
handler: &Handler,
engines: &Engines,
name: Ident,
item: ty::TyDecl,
const_shadowing_mode: ConstShadowingMode,
generic_shadowing_mode: GenericShadowingMode,
collecting_unifications: bool,
) -> Result<(), ErrorEmitted> {
self.insert_symbol(
handler,
engines,
name,
ResolvedDeclaration::Typed(item),
const_shadowing_mode,
generic_shadowing_mode,
collecting_unifications,
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn insert_symbol(
&mut self,
handler: &Handler,
engines: &Engines,
name: Ident,
item: ResolvedDeclaration,
const_shadowing_mode: ConstShadowingMode,
generic_shadowing_mode: GenericShadowingMode,
collecting_unifications: bool,
) -> Result<(), ErrorEmitted> {
let parsed_decl_engine = engines.pe();
let decl_engine = engines.de();
#[allow(unused)]
let append_shadowing_error_parsed =
|ident: &Ident,
decl: &Declaration,
is_use: bool,
is_alias: bool,
item: &Declaration,
const_shadowing_mode: ConstShadowingMode| {
use Declaration::*;
match (
ident,
decl,
is_use,
is_alias,
&item,
const_shadowing_mode,
generic_shadowing_mode,
) {
// A general remark for using the `ShadowingSource::LetVar`.
// If the shadowing is detected at this stage, the variable is for
// sure a local variable, because in the case of pattern matching
// struct field variables, the error is already reported and
// the compilation do not proceed to the point of inserting
// the pattern variable into the items.
// variable shadowing a constant
(
constant_ident,
ConstantDeclaration(decl_id),
is_imported_constant,
is_alias,
VariableDeclaration { .. },
_,
_,
) => {
handler.emit_err(CompileError::ConstantsCannotBeShadowed {
shadowing_source: ShadowingSource::LetVar,
name: (&name).into(),
constant_span: constant_ident.span(),
constant_decl_span: if is_imported_constant {
parsed_decl_engine.get(decl_id).span.clone()
} else {
Span::dummy()
},
is_alias,
});
}
// variable shadowing a configurable
(
configurable_ident,
ConfigurableDeclaration(_),
_,
_,
VariableDeclaration { .. },
_,
_,
) => {
handler.emit_err(CompileError::ConfigurablesCannotBeShadowed {
shadowing_source: ShadowingSource::LetVar,
name: (&name).into(),
configurable_span: configurable_ident.span(),
});
}
// constant shadowing a constant sequentially
(
constant_ident,
ConstantDeclaration(decl_id),
is_imported_constant,
is_alias,
ConstantDeclaration { .. },
ConstShadowingMode::Sequential,
_,
) => {
handler.emit_err(CompileError::ConstantsCannotBeShadowed {
shadowing_source: ShadowingSource::Const,
name: (&name).into(),
constant_span: constant_ident.span(),
constant_decl_span: if is_imported_constant {
parsed_decl_engine.get(decl_id).span.clone()
} else {
Span::dummy()
},
is_alias,
});
}
// constant shadowing a configurable sequentially
(
configurable_ident,
ConfigurableDeclaration(_),
_,
_,
ConstantDeclaration { .. },
ConstShadowingMode::Sequential,
_,
) => {
handler.emit_err(CompileError::ConfigurablesCannotBeShadowed {
shadowing_source: ShadowingSource::Const,
name: (&name).into(),
configurable_span: configurable_ident.span(),
});
}
// constant shadowing a variable
(_, VariableDeclaration(decl_id), _, _, ConstantDeclaration { .. }, _, _) => {
handler.emit_err(CompileError::ConstantShadowsVariable {
name: (&name).into(),
variable_span: parsed_decl_engine.get(decl_id).name.span(),
});
}
// constant shadowing a constant item-style (outside of a function body)
(
constant_ident,
ConstantDeclaration { .. },
_,
_,
ConstantDeclaration { .. },
ConstShadowingMode::ItemStyle,
_,
) => {
handler.emit_err(CompileError::ConstantDuplicatesConstantOrConfigurable {
existing_constant_or_configurable: "Constant",
new_constant_or_configurable: "Constant",
name: (&name).into(),
existing_span: constant_ident.span(),
});
}
// constant shadowing a configurable item-style (outside of a function body)
(
configurable_ident,
ConfigurableDeclaration { .. },
_,
_,
ConstantDeclaration { .. },
ConstShadowingMode::ItemStyle,
_,
) => {
handler.emit_err(CompileError::ConstantDuplicatesConstantOrConfigurable {
existing_constant_or_configurable: "Configurable",
new_constant_or_configurable: "Constant",
name: (&name).into(),
existing_span: configurable_ident.span(),
});
}
// configurable shadowing a constant item-style (outside of a function body)
(
constant_ident,
ConstantDeclaration { .. },
_,
_,
ConfigurableDeclaration { .. },
ConstShadowingMode::ItemStyle,
_,
) => {
handler.emit_err(CompileError::ConstantDuplicatesConstantOrConfigurable {
existing_constant_or_configurable: "Constant",
new_constant_or_configurable: "Configurable",
name: (&name).into(),
existing_span: constant_ident.span(),
});
}
// type or type alias shadowing another type or type alias
// trait/abi shadowing another trait/abi
// type or type alias shadowing a trait/abi, or vice versa
(
_,
StructDeclaration { .. }
| EnumDeclaration { .. }
| TypeAliasDeclaration { .. }
| TraitDeclaration { .. }
| AbiDeclaration { .. },
_,
_,
StructDeclaration { .. }
| EnumDeclaration { .. }
| TypeAliasDeclaration { .. }
| TraitDeclaration { .. }
| AbiDeclaration { .. },
_,
_,
) => {
handler.emit_err(CompileError::MultipleDefinitionsOfName {
name: name.clone(),
span: name.span(),
});
}
_ => {}
}
};
let append_shadowing_error_typed =
|ident: &Ident,
decl: &ty::TyDecl,
is_use: bool,
is_alias: bool,
item: &ty::TyDecl,
const_shadowing_mode: ConstShadowingMode| {
use ty::TyDecl::*;
match (
ident,
decl,
is_use,
is_alias,
&item,
const_shadowing_mode,
generic_shadowing_mode,
) {
// A general remark for using the `ShadowingSource::LetVar`.
// If the shadowing is detected at this stage, the variable is for
// sure a local variable, because in the case of pattern matching
// struct field variables, the error is already reported and
// the compilation do not proceed to the point of inserting
// the pattern variable into the items.
// variable shadowing a constant
(
constant_ident,
ConstantDecl(constant_decl),
is_imported_constant,
is_alias,
VariableDecl { .. },
_,
_,
) => {
handler.emit_err(CompileError::ConstantsCannotBeShadowed {
shadowing_source: ShadowingSource::LetVar,
name: (&name).into(),
constant_span: constant_ident.span(),
constant_decl_span: if is_imported_constant {
decl_engine.get(&constant_decl.decl_id).span.clone()
} else {
Span::dummy()
},
is_alias,
});
}
// variable shadowing a configurable
(configurable_ident, ConfigurableDecl(_), _, _, VariableDecl { .. }, _, _) => {
handler.emit_err(CompileError::ConfigurablesCannotBeShadowed {
shadowing_source: ShadowingSource::LetVar,
name: (&name).into(),
configurable_span: configurable_ident.span(),
});
}
// constant shadowing a constant sequentially
(
constant_ident,
ConstantDecl(constant_decl),
is_imported_constant,
is_alias,
ConstantDecl { .. },
ConstShadowingMode::Sequential,
_,
) => {
handler.emit_err(CompileError::ConstantsCannotBeShadowed {
shadowing_source: ShadowingSource::Const,
name: (&name).into(),
constant_span: constant_ident.span(),
constant_decl_span: if is_imported_constant {
decl_engine.get(&constant_decl.decl_id).span.clone()
} else {
Span::dummy()
},
is_alias,
});
}
// constant shadowing a configurable sequentially
(
configurable_ident,
ConfigurableDecl(_),
_,
_,
ConstantDecl { .. },
ConstShadowingMode::Sequential,
_,
) => {
handler.emit_err(CompileError::ConfigurablesCannotBeShadowed {
shadowing_source: ShadowingSource::Const,
name: (&name).into(),
configurable_span: configurable_ident.span(),
});
}
// constant shadowing a variable
(_, VariableDecl(variable_decl), _, _, ConstantDecl { .. }, _, _) => {
handler.emit_err(CompileError::ConstantShadowsVariable {
name: (&name).into(),
variable_span: variable_decl.name.span(),
});
}
// constant shadowing a constant item-style (outside of a function body)
(
constant_ident,
ConstantDecl { .. },
_,
_,
ConstantDecl { .. },
ConstShadowingMode::ItemStyle,
_,
) => {
handler.emit_err(CompileError::ConstantDuplicatesConstantOrConfigurable {
existing_constant_or_configurable: "Constant",
new_constant_or_configurable: "Constant",
name: (&name).into(),
existing_span: constant_ident.span(),
});
}
// constant shadowing a configurable item-style (outside of a function body)
(
configurable_ident,
ConfigurableDecl { .. },
_,
_,
ConstantDecl { .. },
ConstShadowingMode::ItemStyle,
_,
) => {
handler.emit_err(CompileError::ConstantDuplicatesConstantOrConfigurable {
existing_constant_or_configurable: "Configurable",
new_constant_or_configurable: "Constant",
name: (&name).into(),
existing_span: configurable_ident.span(),
});
}
// configurable shadowing a constant item-style (outside of a function body)
(
constant_ident,
ConstantDecl { .. },
_,
_,
ConfigurableDecl { .. },
ConstShadowingMode::ItemStyle,
_,
) => {
handler.emit_err(CompileError::ConstantDuplicatesConstantOrConfigurable {
existing_constant_or_configurable: "Constant",
new_constant_or_configurable: "Configurable",
name: (&name).into(),
existing_span: constant_ident.span(),
});
}
// type or type alias shadowing another type or type alias
// trait/abi shadowing another trait/abi
// type or type alias shadowing a trait/abi, or vice versa
(
_,
StructDecl { .. }
| EnumDecl { .. }
| TypeAliasDecl { .. }
| TraitDecl { .. }
| AbiDecl { .. },
_,
_,
StructDecl { .. }
| EnumDecl { .. }
| TypeAliasDecl { .. }
| TraitDecl { .. }
| AbiDecl { .. },
_,
_,
) => {
handler.emit_err(CompileError::MultipleDefinitionsOfName {
name: name.clone(),
span: name.span(),
});
}
// generic parameter shadowing another generic parameter
(
_,
GenericTypeForFunctionScope { .. },
_,
_,
GenericTypeForFunctionScope { .. },
_,
GenericShadowingMode::Disallow,
) => {
handler.emit_err(CompileError::GenericShadowsGeneric {
name: (&name).into(),
});
}
_ => {}
}
};
let append_shadowing_error =
|ident: &Ident,
decl: &ResolvedDeclaration,
is_use: bool,
is_alias: bool,
item: &ResolvedDeclaration,
const_shadowing_mode: ConstShadowingMode| {
match (decl, item) {
// TODO: Do not handle any shadowing errors while handling parsed declarations yet,
// or else we will emit errors in a different order from the source code order.
// Update this once the full AST resolving pass is in.
(ResolvedDeclaration::Typed(_decl), ResolvedDeclaration::Parsed(_item)) => {}
(ResolvedDeclaration::Parsed(_decl), ResolvedDeclaration::Parsed(_item)) => {}
(ResolvedDeclaration::Typed(decl), ResolvedDeclaration::Typed(item)) => {
append_shadowing_error_typed(
ident,
decl,
is_use,
is_alias,
item,
const_shadowing_mode,
)
}
_ => unreachable!(),
}
};
if let Some((ident, decl)) = self.symbols.get_key_value(&name) {
append_shadowing_error(
ident,
decl,
false,
false,
&item.clone(),
const_shadowing_mode,
);
}
if let Some((ident, (imported_ident, _, decl, _))) =
self.use_item_synonyms.get_key_value(&name)
{
append_shadowing_error(
ident,
decl,
true,
imported_ident.is_some(),
&item,
const_shadowing_mode,
);
}
if collecting_unifications {
self.symbols_unique_while_collecting_unifications
.write()
.insert(name.clone().into(), item.clone());
}
self.symbols.insert(name, item);
Ok(())
}
// Add a new binding into use_glob_synonyms. The symbol may already be bound by an earlier
// insertion, in which case the new binding is added as well so that multiple bindings exist.
//
// There are a few edge cases were a new binding will replace an old binding. These edge cases
// are a consequence of the prelude reexports not being implemented properly. See comments in
// the code for details.
pub(crate) fn insert_glob_use_symbol(
&mut self,
engines: &Engines,
symbol: Ident,
src_path: ModulePathBuf,
decl: &ResolvedDeclaration,
visibility: Visibility,
) {
if let Some(cur_decls) = self.use_glob_synonyms.get_mut(&symbol) {
// Name already bound. Check if the decl is already imported
let ctx = PartialEqWithEnginesContext::new(engines);
match cur_decls
.iter()
.position(|(_cur_path, cur_decl, _cur_visibility)| cur_decl.eq(decl, &ctx))
{
Some(index) if matches!(visibility, Visibility::Public) => {
// The name is already bound to this decl. If the new symbol is more visible
// than the old one, then replace the old one.
cur_decls[index] = (src_path.to_vec(), decl.clone(), visibility);
}
Some(_) => {
// Same binding as the existing one. Do nothing.
}
None => {
// New decl for this name. Add it to the end
cur_decls.push((src_path.to_vec(), decl.clone(), visibility));
}
}
} else {
let new_vec = vec![(src_path.to_vec(), decl.clone(), visibility)];
self.use_glob_synonyms.insert(symbol, new_vec);
}
}
pub(crate) fn check_symbol(&self, name: &Ident) -> Result<ResolvedDeclaration, CompileError> {
self.symbols
.get(name)
.cloned()
.ok_or_else(|| CompileError::SymbolNotFound {
name: name.clone(),
span: name.span(),
})
}
pub(crate) fn check_symbols_unique_while_collecting_unifications(
&self,
name: &Ident,
) -> Result<ResolvedDeclaration, CompileError> {
self.symbols_unique_while_collecting_unifications
.read()
.get(&name.into())
.cloned()
.ok_or_else(|| CompileError::SymbolNotFound {
name: name.clone(),
span: name.span(),
})
}
pub(crate) fn clear_symbols_unique_while_collecting_unifications(&self) {
self.symbols_unique_while_collecting_unifications
.write()
.clear();
}
pub fn get_items_for_type(
&self,
engines: &Engines,
type_id: TypeId,
) -> Vec<ResolvedTraitImplItem> {
self.implemented_traits.get_items_for_type(engines, type_id)
}
pub fn get_impl_spans_for_decl(&self, engines: &Engines, ty_decl: &TyDecl) -> Vec<Span> {
let handler = Handler::default();
ty_decl
.return_type(&handler, engines)
.map(|type_id| {
self.implemented_traits
.get_impl_spans_for_type(engines, &type_id)
})
.unwrap_or_default()
}
pub fn get_impl_spans_for_type(&self, engines: &Engines, type_id: &TypeId) -> Vec<Span> {
self.implemented_traits
.get_impl_spans_for_type(engines, type_id)
}
pub fn get_impl_spans_for_trait_name(&self, trait_name: &CallPath) -> Vec<Span> {
self.implemented_traits
.get_impl_spans_for_trait_name(trait_name)
}
pub fn get_methods_for_type(
&self,
engines: &Engines,
type_id: TypeId,
) -> Vec<ResolvedFunctionDecl> {
self.get_items_for_type(engines, type_id)
.into_iter()
.filter_map(|item| match item {
ResolvedTraitImplItem::Parsed(_) => todo!(),
ResolvedTraitImplItem::Typed(item) => match item {
ty::TyTraitItem::Fn(decl_ref) => Some(ResolvedFunctionDecl::Typed(decl_ref)),
ty::TyTraitItem::Constant(_decl_ref) => None,
ty::TyTraitItem::Type(_decl_ref) => None,
},
})
.collect::<Vec<_>>()
}
pub(crate) fn has_storage_declared(&self) -> bool {
self.declared_storage.is_some()
}
pub fn get_declared_storage(&self, decl_engine: &DeclEngine) -> Option<TyStorageDecl> {
self.declared_storage
.as_ref()
.map(|decl_ref| (*decl_engine.get_storage(decl_ref)).clone())
}
pub(crate) fn get_storage_field_descriptors(
&self,
handler: &Handler,
decl_engine: &DeclEngine,
) -> Result<Vec<ty::TyStorageField>, ErrorEmitted> {
match self.get_declared_storage(decl_engine) {
Some(storage) => Ok(storage.fields.clone()),
None => {
let msg = "unknown source location";
let span = Span::new(Arc::from(msg), 0, msg.len(), None).unwrap();
Err(handler.emit_err(CompileError::NoDeclaredStorage { span }))
}
}
}
/// Returns a tuple where the first element is the [TypeId] of the actual expression, and
/// the second is the [TypeId] of its parent.
pub(crate) fn find_subfield_type(
&self,
handler: &Handler,
engines: &Engines,
namespace: &Namespace,
base_name: &Ident,
projections: &[ty::ProjectionKind],
) -> Result<(TypeId, TypeId), ErrorEmitted> {
let type_engine = engines.te();
let decl_engine = engines.de();
let symbol = match self.symbols.get(base_name).cloned() {
Some(s) => s,
None => {
return Err(handler.emit_err(CompileError::UnknownVariable {
var_name: base_name.clone(),
span: base_name.span(),
}));
}
};
let mut symbol = match symbol {
ResolvedDeclaration::Parsed(_) => unreachable!(),
ResolvedDeclaration::Typed(ty_decl) => ty_decl.return_type(handler, engines)?,
};
let mut symbol_span = base_name.span();
let mut parent_rover = symbol;
let mut full_span_for_error = base_name.span();
for projection in projections {
let resolved_type = match type_engine.to_typeinfo(symbol, &symbol_span) {
Ok(resolved_type) => resolved_type,
Err(error) => {
return Err(handler.emit_err(CompileError::TypeError(error)));
}
};
match (resolved_type, projection) {
(
TypeInfo::Struct(decl_ref),
ty::ProjectionKind::StructField { name: field_name },
) => {
let struct_decl = decl_engine.get_struct(&decl_ref);
let (struct_can_be_changed, is_public_struct_access) =
StructAccessInfo::get_info(engines, &struct_decl, namespace).into();
let field_type_id = match struct_decl.find_field(field_name) {
Some(struct_field) => {
if is_public_struct_access && struct_field.is_private() {
return Err(handler.emit_err(CompileError::StructFieldIsPrivate {
field_name: field_name.into(),
struct_name: struct_decl.call_path.suffix.clone(),
field_decl_span: struct_field.name.span(),
struct_can_be_changed,
usage_context: StructFieldUsageContext::StructFieldAccess,
}));
}
struct_field.type_argument.type_id
}
None => {
return Err(handler.emit_err(CompileError::StructFieldDoesNotExist {
field_name: field_name.into(),
available_fields: struct_decl
.accessible_fields_names(is_public_struct_access),
is_public_struct_access,
struct_name: struct_decl.call_path.suffix.clone(),
struct_decl_span: struct_decl.span(),
struct_is_empty: struct_decl.is_empty(),
usage_context: StructFieldUsageContext::StructFieldAccess,
}));
}
};
parent_rover = symbol;
symbol = field_type_id;
symbol_span = field_name.span().clone();
full_span_for_error = Span::join(full_span_for_error, &field_name.span());
}
(TypeInfo::Tuple(fields), ty::ProjectionKind::TupleField { index, index_span }) => {
let field_type_opt = {
fields
.get(*index)
.map(|TypeArgument { type_id, .. }| type_id)
};
let field_type = match field_type_opt {
Some(field_type) => field_type,
None => {
return Err(handler.emit_err(CompileError::TupleIndexOutOfBounds {
index: *index,
count: fields.len(),
tuple_type: engines.help_out(symbol).to_string(),
span: index_span.clone(),
prefix_span: full_span_for_error.clone(),
}));
}
};
parent_rover = symbol;
symbol = *field_type;
symbol_span = index_span.clone();
full_span_for_error = Span::join(full_span_for_error, index_span);
}
(
TypeInfo::Array(elem_ty, _),
ty::ProjectionKind::ArrayIndex { index_span, .. },
) => {
parent_rover = symbol;
symbol = elem_ty.type_id;
symbol_span = index_span.clone();
// `index_span` does not contain the enclosing square brackets.
// Which means, if this array index access is the last one before the
// erroneous expression, the `full_span_for_error` will be missing the
// closing `]`. We can live with this small glitch so far. To fix it,
// we would need to bring the full span of the index all the way from
// the parsing stage. An effort that doesn't pay off at the moment.
// TODO: Include the closing square bracket into the error span.
full_span_for_error = Span::join(full_span_for_error, index_span);
}
(actually, ty::ProjectionKind::StructField { name }) => {
return Err(handler.emit_err(CompileError::FieldAccessOnNonStruct {
actually: engines.help_out(actually).to_string(),
storage_variable: None,
field_name: name.into(),
span: full_span_for_error,
}));
}
(
actually,
ty::ProjectionKind::TupleField {
index, index_span, ..
},
) => {
return Err(
handler.emit_err(CompileError::TupleElementAccessOnNonTuple {
actually: engines.help_out(actually).to_string(),
span: full_span_for_error,
index: *index,
index_span: index_span.clone(),
}),
);
}
(actually, ty::ProjectionKind::ArrayIndex { .. }) => {
return Err(handler.emit_err(CompileError::NotIndexable {
actually: engines.help_out(actually).to_string(),
span: full_span_for_error,
}));
}
}
}
Ok((symbol, parent_rover))
}
}
fn get_path_for_decl(
path: &[sway_types::BaseIdent],
decl: &ResolvedDeclaration,
engines: &Engines,
) -> String {
let mut path_names = path.iter().map(|x| x.to_string()).collect::<Vec<_>>();
// Add the enum name to the path if the decl is an enum variant.
match decl {
ResolvedDeclaration::Parsed(decl) => {
if let Declaration::EnumVariantDeclaration(decl) = decl {
let enum_decl = engines.pe().get_enum(&decl.enum_ref);
path_names.push(enum_decl.name().to_string())
};
}
ResolvedDeclaration::Typed(decl) => {
if let TyDecl::EnumVariantDecl(ty::EnumVariantDecl { enum_ref, .. }) = decl {
path_names.push(enum_ref.name().to_string())
};
}
}
path_names.join("::")
}