Skip to content

Commit f76049c

Browse files
committed
Reexport tests without polluting namespaces
1 parent 487e961 commit f76049c

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use codemap::{ExpnInfo, MacroBang, MacroAttribute, dummy_spanned, respan};
1515
use config::{is_test_or_bench, StripUnconfigured};
1616
use errors::{Applicability, FatalError};
1717
use ext::base::*;
18+
use ext::build::AstBuilder;
1819
use ext::derive::{add_derived_markers, collect_derives};
1920
use ext::hygiene::{self, Mark, SyntaxContext};
2021
use ext::placeholders::{placeholder, PlaceholderExpander};
@@ -1354,12 +1355,29 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
13541355
// Ensure that test functions are accessible from the test harness.
13551356
ast::ItemKind::Fn(..) if self.cx.ecfg.should_test => {
13561357
if item.attrs.iter().any(|attr| is_test_or_bench(attr)) {
1358+
let orig_vis = item.vis.clone();
1359+
1360+
// Publicize the item under gensymed name to avoid pollution
13571361
item = item.map(|mut item| {
13581362
item.vis = respan(item.vis.span, ast::VisibilityKind::Public);
1363+
item.ident = Ident::from_interned_str(
1364+
item.ident.as_interned_str()).gensym();
13591365
item
13601366
});
1367+
1368+
// Use the gensymed name under the item's original visibility
1369+
let use_item = self.cx.item_use_simple_(
1370+
item.ident.span,
1371+
orig_vis,
1372+
Some(Ident::from_interned_str(item.ident.as_interned_str())),
1373+
self.cx.path(item.ident.span, vec![item.ident]));
1374+
1375+
SmallVector::many(
1376+
noop_fold_item(item, self).into_iter()
1377+
.chain(noop_fold_item(use_item, self)))
1378+
} else {
1379+
noop_fold_item(item, self)
13611380
}
1362-
noop_fold_item(item, self)
13631381
}
13641382
_ => noop_fold_item(item, self),
13651383
}

src/test/run-pass/issue-52557.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2015 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+
// This test checks for namespace pollution by private tests.
12+
// Tests used to marked as public causing name conflicts with normal
13+
// functions only in test builds.
14+
15+
// compile-flags: --test
16+
17+
mod a {
18+
pub fn foo() -> bool {
19+
true
20+
}
21+
}
22+
23+
mod b {
24+
#[test]
25+
fn foo() {}
26+
}
27+
28+
use a::*;
29+
use b::*;
30+
31+
fn conflict() {
32+
let _: bool = foo();
33+
}

0 commit comments

Comments
 (0)