Skip to content

Commit ebde2ab

Browse files
committed
Deny useing tool paths
1 parent 1767585 commit ebde2ab

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

compiler/rustc_resolve/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,9 @@ resolve_expected_found =
207207
resolve_indeterminate =
208208
cannot determine resolution for the visibility
209209
210+
resolve_tool_module_imported =
211+
cannot use a tool module through an import
212+
.note = the tool module imported here
213+
210214
resolve_module_only =
211215
visibility must resolve to a module

compiler/rustc_resolve/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,15 @@ pub(crate) struct ExpectedFound {
469469
#[diag(resolve_indeterminate, code = "E0578")]
470470
pub(crate) struct Indeterminate(#[primary_span] pub(crate) Span);
471471

472+
#[derive(Diagnostic)]
473+
#[diag(resolve_tool_module_imported)]
474+
pub(crate) struct ToolModuleImported {
475+
#[primary_span]
476+
pub(crate) span: Span,
477+
#[note]
478+
pub(crate) import: Span,
479+
}
480+
472481
#[derive(Diagnostic)]
473482
#[diag(resolve_module_only)]
474483
pub(crate) struct ModuleOnly(#[primary_span] pub(crate) Span);

compiler/rustc_resolve/src/ident.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::late::{
1717
ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
1818
};
1919
use crate::macros::{sub_namespace_match, MacroRulesScope};
20-
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
20+
use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
2121
use crate::{Import, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
2222
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
2323
use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding, Weak};
@@ -1357,7 +1357,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13571357
}
13581358
};
13591359

1360-
let is_last = i == path.len() - 1;
1360+
let is_last = i + 1 == path.len();
13611361
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
13621362
let name = ident.name;
13631363

@@ -1494,16 +1494,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14941494
if let Some(next_module) = binding.module() {
14951495
module = Some(ModuleOrUniformRoot::Module(next_module));
14961496
record_segment_res(self, res);
1497-
} else if res == Res::ToolMod && i + 1 != path.len() {
1497+
} else if res == Res::ToolMod && !is_last && opt_ns.is_some() {
14981498
if binding.is_import() {
1499-
self.tcx
1500-
.sess
1501-
.struct_span_err(
1502-
ident.span,
1503-
"cannot use a tool module through an import",
1504-
)
1505-
.span_note(binding.span, "the tool module imported here")
1506-
.emit();
1499+
self.tcx.sess.emit_err(errors::ToolModuleImported {
1500+
span: ident.span,
1501+
import: binding.span,
1502+
});
15071503
}
15081504
let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
15091505
return PathResult::NonModule(PartialRes::new(res));

tests/ui/resolve/tool-import.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// edition: 2018
2+
3+
use clippy::time::Instant;
4+
//~^ `clippy` is a tool module
5+
6+
fn main() {
7+
Instant::now();
8+
}

tests/ui/resolve/tool-import.stderr

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

0 commit comments

Comments
 (0)