Rollup merge of #109909 - clubby789:import-tool-mod, r=petrochenkov

Deny `use`ing tool paths

Fixes #109853
Fixes #109147
This commit is contained in:
Yuki Okushi 2023-04-06 07:18:29 +09:00 committed by GitHub
commit ea920901e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 11 deletions

View file

@ -17,7 +17,7 @@ use crate::late::{
ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
};
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::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding, Weak};
@ -1364,7 +1364,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 name = ident.name;
@ -1501,16 +1501,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if let Some(next_module) = binding.module() {
module = Some(ModuleOrUniformRoot::Module(next_module));
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() {
self.tcx
.sess
.struct_span_err(
ident.span,
"cannot use a tool module through an import",
)
.span_note(binding.span, "the tool module imported here")
.emit();
self.tcx.sess.emit_err(errors::ToolModuleImported {
span: ident.span,
import: binding.span,
});
}
let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
return PathResult::NonModule(PartialRes::new(res));