Skip to content

Commit ffbf62c

Browse files
committed
Emit a future incompat lint when overwriting the function body
1 parent 2244a91 commit ffbf62c

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+29
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ declare_lint_pass! {
4343
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4444
ELIDED_LIFETIMES_IN_PATHS,
4545
EXPORTED_PRIVATE_DEPENDENCIES,
46+
FEATURE_DETECTION,
4647
FFI_UNWIND_CALLS,
4748
FORBIDDEN_LINT_GROUPS,
4849
FUNCTION_ITEM_REFERENCES,
@@ -176,6 +177,34 @@ declare_lint! {
176177
};
177178
}
178179

180+
declare_lint! {
181+
/// The `feature_detection` lint detects flawed ways to determine
182+
/// whether nightly features can be enabled.
183+
///
184+
/// ### Example
185+
///
186+
/// The `version_check` crate's `supports_feature` function.
187+
///
188+
/// ### Recommended fix
189+
///
190+
/// Remove dependencies that rely on `version_check`.
191+
///
192+
/// ### Explanation
193+
///
194+
/// Detecting if the current compiler is a nightly compiler, without checking if
195+
/// the feature
196+
/// will actually work for the user. This means that updates to the feature
197+
/// will arbitrarily break nightly users that did not opt-in to using nightly
198+
/// features.
199+
pub FEATURE_DETECTION,
200+
Warn,
201+
"applying forbid to lint-groups",
202+
@future_incompatible = FutureIncompatibleInfo {
203+
reason: FutureIncompatibilityReason::Custom("nightly feature detection is disabled"),
204+
reference: "",
205+
};
206+
}
207+
179208
declare_lint! {
180209
/// The `ill_formed_attribute_input` lint detects ill-formed attribute
181210
/// inputs that were previously accepted and used in practice.

compiler/rustc_mir_build/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ mir_build_extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
109109
.note = extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
110110
.label = use of extern static
111111
112+
mir_build_feature_detection = Rust language and library feature detection via `version_check::feature_enabled` does not actually check whether the feature works correctly
113+
.note = the fucntion has been disabled and will always return `None` on a nightly compiler.
114+
112115
mir_build_indirect_structural_match =
113116
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq)]`
114117

compiler/rustc_mir_build/src/build/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::build::expr::as_place::PlaceBuilder;
22
use crate::build::scope::DropKind;
3+
use crate::errors::FeatureDetection;
34
use hir::def_id::LOCAL_CRATE;
45
use itertools::Itertools;
56
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
@@ -21,6 +22,7 @@ use rustc_middle::mir::*;
2122
use rustc_middle::query::TyCtxtAt;
2223
use rustc_middle::thir::{self, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir};
2324
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
25+
use rustc_session::lint;
2426
use rustc_span::symbol::sym;
2527
use rustc_span::Symbol;
2628
use rustc_span::{Span, DUMMY_SP};
@@ -535,6 +537,14 @@ fn construct_fn<'tcx>(
535537
),
536538
);
537539
builder.cfg.terminate(block, source_info, TerminatorKind::Return);
540+
541+
tcx.emit_node_span_lint(
542+
lint::builtin::FEATURE_DETECTION,
543+
fn_id,
544+
span,
545+
FeatureDetection { span },
546+
);
547+
538548
return builder.finish();
539549
}
540550

compiler/rustc_mir_build/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -950,3 +950,10 @@ pub enum RustcBoxAttrReason {
950950
#[note(mir_build_missing_box)]
951951
MissingBox,
952952
}
953+
954+
#[derive(LintDiagnostic)]
955+
#[diag(mir_build_feature_detection)]
956+
pub struct FeatureDetection {
957+
#[note]
958+
pub span: Span,
959+
}

tests/ui/feature-gates/version_check.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//@ run-pass
22

33
fn supports_feature(_: &str) -> Option<bool> {
4+
//~^ WARN:`version_check::feature_enabled` does not actually check
5+
//~| WARN: nightly feature detection is disabled
46
Some(true)
57
}
68

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: Rust language and library feature detection via `version_check::feature_enabled` does not actually check whether the feature works correctly
2+
--> $DIR/version_check.rs:3:1
3+
|
4+
LL | fn supports_feature(_: &str) -> Option<bool> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= warning: nightly feature detection is disabled
8+
= note: for more information, see TODO
9+
note: the fucntion has been disabled and will always return `None` on a nightly compiler.
10+
--> $DIR/version_check.rs:3:1
11+
|
12+
LL | fn supports_feature(_: &str) -> Option<bool> {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
= note: `#[warn(feature_detection)]` on by default
15+
16+
warning: 1 warning emitted
17+

0 commit comments

Comments
 (0)