Skip to content

Commit c0fdddc

Browse files
Move crate type checking later
This allows us to directly pass in a lint buffer
1 parent ea1ff8c commit c0fdddc

File tree

3 files changed

+62
-51
lines changed

3 files changed

+62
-51
lines changed

src/librustc/session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14691469
(such as entering an empty infinite loop) by inserting llvm.sideeffect"),
14701470
}
14711471

1472-
pub fn default_lib_output() -> CrateType {
1472+
pub const fn default_lib_output() -> CrateType {
14731473
CrateType::Rlib
14741474
}
14751475

src/librustc_interface/passes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ fn configure_and_expand_inner<'a>(
295295
krate
296296
});
297297

298+
util::check_attr_crate_type(&krate.attrs, &mut resolver.lint_buffer);
299+
298300
syntax_ext::plugin_macro_defs::inject(
299301
&mut krate, &mut resolver, plugin_info.syntax_exts, sess.edition()
300302
);

src/librustc_interface/util.rs

+59-50
Original file line numberDiff line numberDiff line change
@@ -526,63 +526,72 @@ pub(crate) fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguat
526526
CrateDisambiguator::from(hasher.finish::<Fingerprint>())
527527
}
528528

529+
pub(crate) fn check_attr_crate_type(attrs: &[ast::Attribute], lint_buffer: &mut lint::LintBuffer) {
530+
// Unconditionally collect crate types from attributes to make them used
531+
for a in attrs.iter() {
532+
if a.check_name(sym::crate_type) {
533+
if let Some(n) = a.value_str() {
534+
if let Some(_) = categorize_crate_type(n) {
535+
return;
536+
}
537+
538+
if let ast::MetaItemKind::NameValue(spanned) = a.meta().unwrap().kind {
539+
let span = spanned.span;
540+
let lev_candidate = find_best_match_for_name(
541+
CRATE_TYPES.iter().map(|(k, _)| k),
542+
&n.as_str(),
543+
None
544+
);
545+
if let Some(candidate) = lev_candidate {
546+
lint_buffer.buffer_lint_with_diagnostic(
547+
lint::builtin::UNKNOWN_CRATE_TYPES,
548+
ast::CRATE_NODE_ID,
549+
span,
550+
"invalid `crate_type` value",
551+
lint::builtin::BuiltinLintDiagnostics::
552+
UnknownCrateTypes(
553+
span,
554+
"did you mean".to_string(),
555+
format!("\"{}\"", candidate)
556+
)
557+
);
558+
} else {
559+
lint_buffer.buffer_lint(
560+
lint::builtin::UNKNOWN_CRATE_TYPES,
561+
ast::CRATE_NODE_ID,
562+
span,
563+
"invalid `crate_type` value"
564+
);
565+
}
566+
}
567+
}
568+
}
569+
}
570+
}
571+
572+
const CRATE_TYPES: &[(Symbol, config::CrateType)] = &[
573+
(sym::rlib, config::CrateType::Rlib),
574+
(sym::dylib, config::CrateType::Dylib),
575+
(sym::cdylib, config::CrateType::Cdylib),
576+
(sym::lib, config::default_lib_output()),
577+
(sym::staticlib, config::CrateType::Staticlib),
578+
(sym::proc_dash_macro, config::CrateType::ProcMacro),
579+
(sym::bin, config::CrateType::Executable),
580+
];
581+
582+
fn categorize_crate_type(s: Symbol) -> Option<config::CrateType> {
583+
Some(CRATE_TYPES.iter().find(|(key, _)| *key == s)?.1)
584+
}
585+
529586
pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<config::CrateType> {
530587
// Unconditionally collect crate types from attributes to make them used
531588
let attr_types: Vec<config::CrateType> = attrs
532589
.iter()
533590
.filter_map(|a| {
534591
if a.check_name(sym::crate_type) {
535592
match a.value_str() {
536-
Some(sym::rlib) => Some(config::CrateType::Rlib),
537-
Some(sym::dylib) => Some(config::CrateType::Dylib),
538-
Some(sym::cdylib) => Some(config::CrateType::Cdylib),
539-
Some(sym::lib) => Some(config::default_lib_output()),
540-
Some(sym::staticlib) => Some(config::CrateType::Staticlib),
541-
Some(sym::proc_dash_macro) => Some(config::CrateType::ProcMacro),
542-
Some(sym::bin) => Some(config::CrateType::Executable),
543-
Some(n) => {
544-
let crate_types = vec![
545-
sym::rlib,
546-
sym::dylib,
547-
sym::cdylib,
548-
sym::lib,
549-
sym::staticlib,
550-
sym::proc_dash_macro,
551-
sym::bin
552-
];
553-
554-
if let ast::MetaItemKind::NameValue(spanned) = a.meta().unwrap().kind {
555-
let span = spanned.span;
556-
let lev_candidate = find_best_match_for_name(
557-
crate_types.iter(),
558-
&n.as_str(),
559-
None
560-
);
561-
if let Some(candidate) = lev_candidate {
562-
session.buffer_lint_with_diagnostic_late(
563-
lint::builtin::UNKNOWN_CRATE_TYPES,
564-
ast::CRATE_NODE_ID,
565-
span,
566-
"invalid `crate_type` value",
567-
lint::builtin::BuiltinLintDiagnostics::
568-
UnknownCrateTypes(
569-
span,
570-
"did you mean".to_string(),
571-
format!("\"{}\"", candidate)
572-
)
573-
);
574-
} else {
575-
session.buffer_lint_late(
576-
lint::builtin::UNKNOWN_CRATE_TYPES,
577-
ast::CRATE_NODE_ID,
578-
span,
579-
"invalid `crate_type` value"
580-
);
581-
}
582-
}
583-
None
584-
}
585-
None => None
593+
Some(s) => categorize_crate_type(s),
594+
_ => None,
586595
}
587596
} else {
588597
None

0 commit comments

Comments
 (0)