| @@ -21,7 +21,7 @@ pub mod def; |
| 21 | 21 | mod externs; |
| 22 | 22 | mod lower; |
| 23 | 23 | |
| 24 | | −use std::collections::HashMap; |
| 24 | +use std::collections::{HashMap, HashSet}; |
| 25 | 25 | |
| 26 | 26 | use louise_diag::Diagnostic; |
| 27 | 27 | use louise_span::{FileId, Span}; |
| @@ -59,6 +59,10 @@ pub mod codes { |
| 59 | 59 | pub const NOT_A_TRAIT_BINDING: &str = "E0121"; |
| 60 | 60 | pub const HTML_EVENT_RESERVED: &str = "E0603"; |
| 61 | 61 | pub const COMPONENT_FLAG: &str = "E0604"; |
| 62 | + /// A local binding takes the name of an imported module (issue #297). |
| 63 | + pub const SHADOWED_MODULE: &str = "W0101"; |
| 64 | + /// An import nothing uses (issue #297). |
| 65 | + pub const UNUSED_IMPORT: &str = "W0102"; |
| 62 | 66 | } |
| 63 | 67 | |
| 64 | 68 | /// One source module of a package. |
| @@ -77,6 +81,35 @@ pub struct Scope { |
| 77 | 81 | pub modules: HashMap<String, ModuleId>, |
| 78 | 82 | } |
| 79 | 83 | |
| 84 | +/// One `import` statement, resolved (for the unused-import lint, W0102). |
| 85 | +#[derive(Debug)] |
| 86 | +pub struct Import { |
| 87 | + /// The module that writes it. |
| 88 | + pub module: ModuleId, |
| 89 | + /// The module it brings in. |
| 90 | + pub target: ModuleId, |
| 91 | + /// The name the module is bound to (the `as` alias, else the path's |
| 92 | + /// last segment). |
| 93 | + pub alias: String, |
| 94 | + /// Where the path is written. |
| 95 | + pub span: Span, |
| 96 | + /// The `.{...}` items that resolved, if any. |
| 97 | + pub items: Vec<DefId>, |
| 98 | +} |
| 99 | + |
| 100 | +/// What lowering resolved through a module's imports, for the |
| 101 | +/// unused-import lint (W0102, issue #297). |
| 102 | +#[derive(Default)] |
| 103 | +pub(crate) struct ImportUse { |
| 104 | + /// `(importing, imported)` a qualified access (`m.item`) resolved |
| 105 | + /// through. Two aliases of one module cannot be told apart: using one |
| 106 | + /// marks both. |
| 107 | + pub modules: HashSet<(ModuleId, ModuleId)>, |
| 108 | + /// `(module, def)`: a definition referred to by name in `module`; |
| 109 | + /// those that came in through an `import m.{...}` mark it used. |
| 110 | + pub defs: HashSet<(ModuleId, DefId)>, |
| 111 | +} |
| 112 | + |
| 80 | 113 | #[derive(Default, Debug)] |
| 81 | 114 | pub struct Hir { |
| 82 | 115 | pub modules: Arena<ModuleData>, |
| @@ -87,6 +120,8 @@ pub struct Hir { |
| 87 | 120 | pub exports: Vec<Scope>, |
| 88 | 121 | /// Items visible everywhere without import. |
| 89 | 122 | pub prelude: Option<ModuleId>, |
| 123 | + /// Every resolved `import` statement (for W0102). |
| 124 | + pub imports: Vec<Import>, |
| 90 | 125 | } |
| 91 | 126 | |
| 92 | 127 | impl Hir { |
| @@ -192,7 +227,8 @@ pub fn lower_package(sources: &[SourceModule], prelude: Option<&str>) -> (Hir, V |
| 192 | 227 | resolve_imports(&mut hir, module, &src.parse.tree(), &mut diags); |
| 193 | 228 | } |
| 194 | 229 | check_import_cycles(&hir, sources, &mut diags); |
| 195 | | − lower::lower_pending(&mut hir, pending, &mut diags); |
| 230 | + let used = lower::lower_pending(&mut hir, pending, &mut diags); |
| 231 | + warn_unused_imports(&hir, &used, &mut diags); |
| 196 | 232 | externs::check(&hir, &mut diags); |
| 197 | 233 | (hir, diags) |
| 198 | 234 | } |
| @@ -674,8 +710,11 @@ fn resolve_imports( |
| 674 | 710 | continue; |
| 675 | 711 | }; |
| 676 | 712 | let alias = import.alias().map_or_else(|| last.text(), |a| a.text()); |
| 677 | | − hir.scopes[module.index()].modules.insert(alias, target); |
| 713 | + hir.scopes[module.index()] |
| 714 | + .modules |
| 715 | + .insert(alias.clone(), target); |
| 678 | 716 | |
| 717 | + let mut items_resolved = Vec::new(); |
| 679 | 718 | for imported in import.items() { |
| 680 | 719 | let Some(name_ref) = imported.name_ref() else { |
| 681 | 720 | continue; |
| @@ -741,11 +780,20 @@ fn resolve_imports( |
| 741 | 780 | ) |
| 742 | 781 | .with_label(hir.defs[existing].span, "defined here"), |
| 743 | 782 | ); |
| 783 | + continue; |
| 744 | 784 | } |
| 745 | | − continue; |
| 785 | + } else { |
| 786 | + entries.insert(local_name, def); |
| 746 | 787 | } |
| 747 | | − entries.insert(local_name, def); |
| 788 | + items_resolved.push(def); |
| 748 | 789 | } |
| 790 | + hir.imports.push(Import { |
| 791 | + module, |
| 792 | + target, |
| 793 | + alias, |
| 794 | + span: path_span, |
| 795 | + items: items_resolved, |
| 796 | + }); |
| 749 | 797 | } |
| 750 | 798 | } |
| 751 | 799 | |
| @@ -814,3 +862,51 @@ fn check_import_cycles(hir: &Hir, sources: &[SourceModule], diags: &mut Vec<Diag |
| 814 | 862 | } |
| 815 | 863 | } |
| 816 | 864 | } |
| 865 | + |
| 866 | +/// W0102 (issue #297): an import nothing uses — no `alias.item` access |
| 867 | +/// resolved through it, and none of its `.{...}` items is referred to. |
| 868 | +/// The prelude needs no import, so it is not here. A trait or derive |
| 869 | +/// function named by an attribute (`@derive(T)`, `@derivable(f)`) is |
| 870 | +/// resolved by the type checker, after lowering; count it as a use here. |
| 871 | +fn warn_unused_imports(hir: &Hir, used: &ImportUse, diags: &mut Vec<Diagnostic>) { |
| 872 | + let mut defs = used.defs.clone(); |
| 873 | + for (_, data) in hir.defs.iter() { |
| 874 | + for name in &data.attrs.derive { |
| 875 | + if let Some(def) = hir.lookup_type(data.module, name) { |
| 876 | + defs.insert((data.module, def)); |
| 877 | + } |
| 878 | + } |
| 879 | + if let Some(attrs::Derivable::Function(f)) = &data.attrs.derivable |
| 880 | + && let Some(def) = hir.lookup_value(data.module, f) |
| 881 | + { |
| 882 | + defs.insert((data.module, def)); |
| 883 | + } |
| 884 | + } |
| 885 | + for import in &hir.imports { |
| 886 | + if used.modules.contains(&(import.module, import.target)) |
| 887 | + || import |
| 888 | + .items |
| 889 | + .iter() |
| 890 | + .any(|d| defs.contains(&(import.module, *d))) |
| 891 | + { |
| 892 | + continue; |
| 893 | + } |
| 894 | + let path = &hir.modules[import.target].path; |
| 895 | + let note = if import.items.is_empty() { |
| 896 | + format!( |
| 897 | + "use its items as `{}.item`, or remove the import", |
| 898 | + import.alias |
| 899 | + ) |
| 900 | + } else { |
| 901 | + "use the imported names, or remove the import".to_owned() |
| 902 | + }; |
| 903 | + diags.push( |
| 904 | + Diagnostic::warning( |
| 905 | + codes::UNUSED_IMPORT, |
| 906 | + format!("`{path}` is imported but never used"), |
| 907 | + import.span, |
| 908 | + ) |
| 909 | + .with_note(note), |
| 910 | + ); |
| 911 | + } |
| 912 | +} |