Skip to content

Commit d4beeca

Browse files
Move validate_crate_name to rustc_metadata
1 parent 88e0ff1 commit d4beeca

File tree

10 files changed

+75
-30
lines changed

10 files changed

+75
-30
lines changed

src/Cargo.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,7 @@ dependencies = [
22192219
"rustc 0.0.0",
22202220
"rustc_data_structures 0.0.0",
22212221
"rustc_incremental 0.0.0",
2222+
"rustc_metadata_utils 0.0.0",
22222223
"rustc_mir 0.0.0",
22232224
"rustc_target 0.0.0",
22242225
"syntax 0.0.0",
@@ -2352,13 +2353,22 @@ dependencies = [
23522353
"rustc 0.0.0",
23532354
"rustc_data_structures 0.0.0",
23542355
"rustc_errors 0.0.0",
2356+
"rustc_metadata_utils 0.0.0",
23552357
"rustc_target 0.0.0",
23562358
"serialize 0.0.0",
23572359
"syntax 0.0.0",
23582360
"syntax_ext 0.0.0",
23592361
"syntax_pos 0.0.0",
23602362
]
23612363

2364+
[[package]]
2365+
name = "rustc_metadata_utils"
2366+
version = "0.0.0"
2367+
dependencies = [
2368+
"rustc 0.0.0",
2369+
"syntax_pos 0.0.0",
2370+
]
2371+
23622372
[[package]]
23632373
name = "rustc_mir"
23642374
version = "0.0.0"

src/librustc/middle/cstore.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -260,33 +260,6 @@ pub trait CrateStore {
260260

261261
pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
262262

263-
// FIXME: find a better place for this?
264-
pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
265-
let mut err_count = 0;
266-
{
267-
let mut say = |s: &str| {
268-
match (sp, sess) {
269-
(_, None) => bug!("{}", s),
270-
(Some(sp), Some(sess)) => sess.span_err(sp, s),
271-
(None, Some(sess)) => sess.err(s),
272-
}
273-
err_count += 1;
274-
};
275-
if s.is_empty() {
276-
say("crate name must not be empty");
277-
}
278-
for c in s.chars() {
279-
if c.is_alphanumeric() { continue }
280-
if c == '_' { continue }
281-
say(&format!("invalid character `{}` in crate name: `{}`", c, s));
282-
}
283-
}
284-
285-
if err_count > 0 {
286-
sess.unwrap().abort_if_errors();
287-
}
288-
}
289-
290263
/// A dummy crate store that does not support any non-local crates,
291264
/// for test purposes.
292265
pub struct DummyCrateStore;

src/librustc_codegen_utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ rustc_target = { path = "../librustc_target" }
2020
rustc_data_structures = { path = "../librustc_data_structures" }
2121
rustc_mir = { path = "../librustc_mir" }
2222
rustc_incremental = { path = "../librustc_incremental" }
23+
rustc_metadata_utils = { path = "../librustc_metadata_utils" }

src/librustc_codegen_utils/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern crate rustc_incremental;
3737
extern crate syntax;
3838
extern crate syntax_pos;
3939
#[macro_use] extern crate rustc_data_structures;
40+
extern crate rustc_metadata_utils;
4041

4142
use rustc::ty::TyCtxt;
4243

src/librustc_codegen_utils/link.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
use rustc::session::config::{self, OutputFilenames, Input, OutputType};
1212
use rustc::session::Session;
13-
use rustc::middle::cstore::{self, LinkMeta};
13+
use rustc::middle::cstore::LinkMeta;
1414
use rustc::hir::svh::Svh;
1515
use std::path::{Path, PathBuf};
1616
use syntax::{ast, attr};
1717
use syntax_pos::Span;
18+
use rustc_metadata_utils::validate_crate_name;
1819

1920
pub fn out_filename(sess: &Session,
2021
crate_type: config::CrateType,
@@ -61,7 +62,7 @@ pub fn find_crate_name(sess: Option<&Session>,
6162
attrs: &[ast::Attribute],
6263
input: &Input) -> String {
6364
let validate = |s: String, span: Option<Span>| {
64-
cstore::validate_crate_name(sess, &s, span);
65+
validate_crate_name(sess, &s, span);
6566
s
6667
};
6768

src/librustc_metadata/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ serialize = { path = "../libserialize" }
2020
syntax = { path = "../libsyntax" }
2121
syntax_ext = { path = "../libsyntax_ext" }
2222
syntax_pos = { path = "../libsyntax_pos" }
23+
rustc_metadata_utils = { path = "../librustc_metadata_utils" }

src/librustc_metadata/creader.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ use rustc::session::config::{Sanitizer, self};
2525
use rustc_target::spec::{PanicStrategy, TargetTriple};
2626
use rustc::session::search_paths::PathKind;
2727
use rustc::middle;
28-
use rustc::middle::cstore::{validate_crate_name, ExternCrate, ExternCrateSource};
28+
use rustc::middle::cstore::{ExternCrate, ExternCrateSource};
2929
use rustc::util::common::record_time;
3030
use rustc::util::nodemap::FxHashSet;
3131
use rustc::hir::map::Definitions;
3232

33+
use rustc_metadata_utils::validate_crate_name;
34+
3335
use std::ops::Deref;
3436
use std::path::PathBuf;
3537
use std::{cmp, fs};

src/librustc_metadata/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern crate serialize as rustc_serialize; // used by deriving
3737
extern crate rustc_errors as errors;
3838
extern crate syntax_ext;
3939
extern crate proc_macro;
40+
extern crate rustc_metadata_utils;
4041

4142
#[macro_use]
4243
extern crate rustc;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
authors = ["The Rust Project Developers"]
3+
name = "rustc_metadata_utils"
4+
version = "0.0.0"
5+
6+
[lib]
7+
name = "rustc_metadata_utils"
8+
path = "lib.rs"
9+
crate-type = ["dylib"]
10+
11+
[dependencies]
12+
rustc = { path = "../librustc" }
13+
syntax_pos = { path = "../libsyntax_pos" }

src/librustc_metadata_utils/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2018 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+
#[macro_use]
12+
extern crate rustc;
13+
extern crate syntax_pos;
14+
15+
use rustc::session::Session;
16+
use syntax_pos::Span;
17+
18+
pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
19+
let mut err_count = 0;
20+
{
21+
let mut say = |s: &str| {
22+
match (sp, sess) {
23+
(_, None) => bug!("{}", s),
24+
(Some(sp), Some(sess)) => sess.span_err(sp, s),
25+
(None, Some(sess)) => sess.err(s),
26+
}
27+
err_count += 1;
28+
};
29+
if s.is_empty() {
30+
say("crate name must not be empty");
31+
}
32+
for c in s.chars() {
33+
if c.is_alphanumeric() { continue }
34+
if c == '_' { continue }
35+
say(&format!("invalid character `{}` in crate name: `{}`", c, s));
36+
}
37+
}
38+
39+
if err_count > 0 {
40+
sess.unwrap().abort_if_errors();
41+
}
42+
}

0 commit comments

Comments
 (0)