Skip to content

Commit 5807fbb

Browse files
committed
Auto merge of #32134 - jseyfried:forbid_type_alias_as_module, r=nikomatsakis
Forbid glob-importing from a type alias This PR forbids glob-importing from a type alias or trait (fixes #30560): ```rust type Alias = (); use Alias::*; // This is currently allowed but shouldn't be ``` This is a [breaking-change]. Since the disallowed glob imports don't actually import anything, any breakage can be fixed by removing the offending glob import. r? @alexcrichton
2 parents aeb85a9 + 1a6092e commit 5807fbb

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
330330

331331
// These items live in the type namespace.
332332
ItemTy(..) => {
333-
let parent_link = ModuleParentLink(parent, name);
334333
let def = Def::TyAlias(self.ast_map.local_def_id(item.id));
335-
let module = self.new_module(parent_link, Some(def), false, is_public);
336-
self.define(parent, name, TypeNS, (module, sp));
334+
self.define(parent, name, TypeNS, (def, sp, modifiers));
337335
parent
338336
}
339337

@@ -495,7 +493,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
495493
}
496494

497495
match def {
498-
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) | Def::TyAlias(..) => {
496+
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) => {
499497
debug!("(building reduced graph for external crate) building module {} {}",
500498
final_ident,
501499
is_public);
@@ -548,7 +546,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
548546
let module = self.new_module(parent_link, Some(def), true, is_public);
549547
self.try_define(new_parent, name, TypeNS, (module, DUMMY_SP));
550548
}
551-
Def::AssociatedTy(..) => {
549+
Def::TyAlias(..) | Def::AssociatedTy(..) => {
552550
debug!("(building reduced graph for external crate) building type {}",
553551
final_ident);
554552
self.try_define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers));

src/librustc_resolve/resolve_imports.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
607607
target_module: Module<'b>,
608608
directive: &'b ImportDirective)
609609
-> ResolveResult<()> {
610+
if let Some(Def::Trait(_)) = target_module.def {
611+
self.resolver.session.span_err(directive.span, "items in traits are not importable.");
612+
}
613+
610614
if module_.def_id() == target_module.def_id() {
611615
// This means we are trying to glob import a module into itself, and it is a no-go
612616
let msg = "Cannot glob-import a module into itself.".into();

src/test/compile-fail/issue-30560.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
type Alias = ();
12+
use Alias::*; //~ ERROR Not a module
13+
use std::io::Result::*; //~ ERROR Not a module
14+
15+
trait T {}
16+
use T::*; //~ ERROR items in traits are not importable
17+
18+
fn main() {}

0 commit comments

Comments
 (0)