Deny useing tool paths

This commit is contained in:
clubby789 2023-04-03 21:30:13 +01:00
parent 1767585509
commit ebde2ab363
5 changed files with 37 additions and 11 deletions

View File

@ -207,5 +207,9 @@ resolve_expected_found =
resolve_indeterminate = resolve_indeterminate =
cannot determine resolution for the visibility cannot determine resolution for the visibility
resolve_tool_module_imported =
cannot use a tool module through an import
.note = the tool module imported here
resolve_module_only = resolve_module_only =
visibility must resolve to a module visibility must resolve to a module

View File

@ -469,6 +469,15 @@ pub(crate) struct ExpectedFound {
#[diag(resolve_indeterminate, code = "E0578")] #[diag(resolve_indeterminate, code = "E0578")]
pub(crate) struct Indeterminate(#[primary_span] pub(crate) Span); pub(crate) struct Indeterminate(#[primary_span] pub(crate) Span);
#[derive(Diagnostic)]
#[diag(resolve_tool_module_imported)]
pub(crate) struct ToolModuleImported {
#[primary_span]
pub(crate) span: Span,
#[note]
pub(crate) import: Span,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(resolve_module_only)] #[diag(resolve_module_only)]
pub(crate) struct ModuleOnly(#[primary_span] pub(crate) Span); pub(crate) struct ModuleOnly(#[primary_span] pub(crate) Span);

View File

@ -17,7 +17,7 @@ use crate::late::{
ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind, ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
}; };
use crate::macros::{sub_namespace_match, MacroRulesScope}; use crate::macros::{sub_namespace_match, MacroRulesScope};
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize}; use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
use crate::{Import, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot}; use crate::{Import, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res}; use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding, Weak}; use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding, Weak};
@ -1357,7 +1357,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} }
}; };
let is_last = i == path.len() - 1; let is_last = i + 1 == path.len();
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS }; let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
let name = ident.name; let name = ident.name;
@ -1494,16 +1494,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if let Some(next_module) = binding.module() { if let Some(next_module) = binding.module() {
module = Some(ModuleOrUniformRoot::Module(next_module)); module = Some(ModuleOrUniformRoot::Module(next_module));
record_segment_res(self, res); record_segment_res(self, res);
} else if res == Res::ToolMod && i + 1 != path.len() { } else if res == Res::ToolMod && !is_last && opt_ns.is_some() {
if binding.is_import() { if binding.is_import() {
self.tcx self.tcx.sess.emit_err(errors::ToolModuleImported {
.sess span: ident.span,
.struct_span_err( import: binding.span,
ident.span, });
"cannot use a tool module through an import",
)
.span_note(binding.span, "the tool module imported here")
.emit();
} }
let res = Res::NonMacroAttr(NonMacroAttrKind::Tool); let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
return PathResult::NonModule(PartialRes::new(res)); return PathResult::NonModule(PartialRes::new(res));

View File

@ -0,0 +1,8 @@
// edition: 2018
use clippy::time::Instant;
//~^ `clippy` is a tool module
fn main() {
Instant::now();
}

View File

@ -0,0 +1,9 @@
error[E0433]: failed to resolve: `clippy` is a tool module, not a module
--> $DIR/tool-import.rs:3:5
|
LL | use clippy::time::Instant;
| ^^^^^^ `clippy` is a tool module, not a module
error: aborting due to previous error
For more information about this error, try `rustc --explain E0433`.