Skip to content

Commit a2f2abf

Browse files
committed
forbid namespaced crate names with more than two segments
sq
1 parent c1abd80 commit a2f2abf

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

compiler/rustc_resolve/src/ident.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
211211
MacroRulesScope::Empty => Scope::ModuleNonGlobs(module.unwrap(), None),
212212
},
213213
Scope::ModuleNonGlobs(module, lint_id) => Scope::ModuleGlobs(module, lint_id),
214-
Scope::ModuleGlobs(module, lint_id) if module_only => {
215-
Scope::NamespacedCrates(NamespacedCrateRoot::Mod(module), lint_id)
216-
}
217-
Scope::NamespacedCrates(..) if module_only | namespace_crate_only => break,
218-
Scope::ModuleGlobs(module, lint_id) if module_and_extern_prelude => {
219-
Scope::NamespacedCrates(NamespacedCrateRoot::Mod(module), lint_id)
220-
}
221-
Scope::NamespacedCrates(..) if module_and_extern_prelude => match ns {
214+
Scope::ModuleGlobs(module, lint_id) if module_only => break,
215+
Scope::ModuleGlobs(module, lint_id) if module_and_extern_prelude => match ns {
222216
TypeNS => {
223217
ctxt.update_unchecked(|ctxt| ctxt.adjust(ExpnId::root()));
224218
Scope::ExternPreludeItems
225219
}
226220
ValueNS | MacroNS => break,
227221
},
228222
Scope::ModuleGlobs(module, prev_lint_id) => {
229-
Scope::NamespacedCrates(NamespacedCrateRoot::Mod(module), prev_lint_id)
230-
}
231-
Scope::NamespacedCrates(namespace_crate_root, prev_lint_id) => {
232-
let module = namespace_crate_root.get_mod();
233223
use_prelude = !module.no_implicit_prelude;
234224
match self.hygienic_lexical_parent(module, &mut ctxt, derive_fallback_lint_id) {
235225
Some((parent_module, lint_id)) => {
@@ -245,6 +235,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
245235
}
246236
}
247237
}
238+
Scope::NamespacedCrates(..) => {
239+
assert!(namespace_crate_only);
240+
break;
241+
}
248242
Scope::MacroUsePrelude => Scope::StdLibPrelude,
249243
Scope::BuiltinAttrs => break, // nowhere else to search
250244
Scope::ExternPreludeItems => Scope::ExternPreludeFlags,

compiler/rustc_session/src/config/externs.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ pub(crate) fn split_extern_opt<'a>(
4343
}
4444
};
4545

46+
// Reject paths with more than two segments.
47+
if unstable_opts.namespaced_crates && crate_name.split("::").count() > 2 {
48+
return Err(early_dcx.early_struct_fatal(format!(
49+
"crate name `{crate_name}` passed to `--extern` has too many segments; namespaced crates currently support at most two segments"
50+
)));
51+
}
52+
4653
if !valid_crate_name(&crate_name, unstable_opts) {
4754
let mut error = early_dcx.early_struct_fatal(format!(
4855
"crate name `{crate_name}` passed to `--extern` is not a valid ASCII identifier"

0 commit comments

Comments
 (0)