Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate #![crate_type] and #![crate_name] #83676

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2961,3 +2961,23 @@ impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations {
}
}
}

pub(crate) struct DeprecatedCrateInfoAttrs;

impl_lint_pass!(DeprecatedCrateInfoAttrs => [DEPRECATED_CRATE_INFO_ATTRS]);

impl EarlyLintPass for DeprecatedCrateInfoAttrs {
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &ast::Crate) {
for attr in &krate.attrs {
if attr.has_name(sym::crate_type) {
cx.struct_span_lint(DEPRECATED_CRATE_INFO_ATTRS, attr.span, |lint| {
lint.build("using deprecated `#![crate_type]` attribute").emit();
})
} else if attr.has_name(sym::crate_name) {
cx.struct_span_lint(DEPRECATED_CRATE_INFO_ATTRS, attr.span, |lint| {
lint.build("using deprecated `#![crate_name]` attribute").emit();
})
}
}
}
}
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ macro_rules! early_lint_passes {
IncompleteFeatures: IncompleteFeatures,
RedundantSemicolons: RedundantSemicolons,
UnusedDocComment: UnusedDocComment,
DeprecatedCrateInfoAttrs: DeprecatedCrateInfoAttrs,
]
);
};
Expand Down
48 changes: 48 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2959,6 +2959,7 @@ declare_lint_pass! {
DISJOINT_CAPTURE_DROP_REORDER,
LEGACY_DERIVE_HELPERS,
PROC_MACRO_BACK_COMPAT,
DEPRECATED_CRATE_INFO_ATTRS,
]
}

Expand Down Expand Up @@ -3136,3 +3137,50 @@ declare_lint! {
})
};
}

declare_lint! {
/// The `deprecated_crate_info_attrs` lint detects uses of the `#![crate_type]`
/// and `#![crate_name]` attributes to specify the crate type and name in the
/// source code.
///
/// ### Example
///
/// ```rust
/// #![crate_type = "lib"]
/// ```
///
/// This will produce:
///
/// ```text
/// warning: using deprecated `#![crate_type]` attribute
/// ::: $DIR/group-compat-hack.rs:1:5
/// |
/// LL | #![crate_type = "lib"]
/// | ^^^^^^^^^^^^^^^^^^^^^^
/// |
/// = note: `#[warn(deprecated_crate_info_attrs)]` on by default
/// = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
/// = note: for more information, see issue #XXXXX <https://github.com/rust-lang/rust/issues/XXXXX>
/// ```
///
/// ### Explanation
///
/// The `#![crate_type]` and `#![crate_name]` attributes require a hack in the
/// compiler to be able to change the used crate type and crate name after the
/// program has been parsed. Neither attribute works in combination with Cargo
/// as it explicitly passes `--crate-type` and `--crate-name` on the commandline,
/// which must match to the value used in the source code.
///
/// To fix the warning use `--crate-type` on the commandline when running rustc
/// instead of `#![crate_type]` and `--crate-name` instead of `#![crate_name]`.
pub DEPRECATED_CRATE_INFO_ATTRS,
Warn,
"detects deprecated usage of #![crate_type] and #![crate_name]",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #XXXXX <https://github.com/rust-lang/rust/issues/XXXXX>", // FIXME
edition: None,
future_breakage: Some(FutureBreakage {
date: None
})
};
}
1 change: 0 additions & 1 deletion library/rtstartup/rsbegin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#![feature(no_core)]
#![feature(lang_items)]
#![feature(auto_traits)]
#![crate_type = "rlib"]
#![no_core]
#![allow(non_camel_case_types)]

Expand Down
1 change: 0 additions & 1 deletion library/rtstartup/rsend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![feature(no_core)]
#![feature(lang_items)]
#![feature(auto_traits)]
#![crate_type = "rlib"]
#![no_core]

#[lang = "sized"]
Expand Down
1 change: 0 additions & 1 deletion library/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// this crate, which relies on this attribute (rather than the value of `--crate-name` passed by
// cargo) to detect this crate.

#![crate_name = "test"]
#![unstable(feature = "test", issue = "50297")]
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))]
#![cfg_attr(unix, feature(libc))]
Expand Down
2 changes: 2 additions & 0 deletions src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ impl Step for StartupObjects {
.arg("--target")
.arg(target.rustc_target_arg())
.arg("--emit=obj")
.arg("--crate-type")
.arg("rlib")
.arg("-o")
.arg(dst_file)
.arg(src_file),
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions src/test/ui/attributes/deprecated-crate-info-attrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#![crate_type = "lib"] //~WARN using deprecated `#![crate_type]` attribute
#![crate_name = "foo"] //~WARN using deprecated `#![crate_name]` attribute
Empty file.
File renamed without changes.
1 change: 0 additions & 1 deletion src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![crate_name = "compiletest"]
// The `test` crate is the only unstable feature
// allowed here, just to share similar code.
#![feature(test)]
Expand Down