Skip to content

Commit

Permalink
Emit a future incompat lint when overwriting the function body
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Apr 24, 2024
1 parent 2244a91 commit ffbf62c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
29 changes: 29 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ declare_lint_pass! {
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
ELIDED_LIFETIMES_IN_PATHS,
EXPORTED_PRIVATE_DEPENDENCIES,
FEATURE_DETECTION,
FFI_UNWIND_CALLS,
FORBIDDEN_LINT_GROUPS,
FUNCTION_ITEM_REFERENCES,
Expand Down Expand Up @@ -176,6 +177,34 @@ declare_lint! {
};
}

declare_lint! {
/// The `feature_detection` lint detects flawed ways to determine
/// whether nightly features can be enabled.
///
/// ### Example
///
/// The `version_check` crate's `supports_feature` function.
///
/// ### Recommended fix
///
/// Remove dependencies that rely on `version_check`.
///
/// ### Explanation
///
/// Detecting if the current compiler is a nightly compiler, without checking if
/// the feature
/// will actually work for the user. This means that updates to the feature
/// will arbitrarily break nightly users that did not opt-in to using nightly
/// features.
pub FEATURE_DETECTION,
Warn,
"applying forbid to lint-groups",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::Custom("nightly feature detection is disabled"),
reference: "",
};
}

declare_lint! {
/// The `ill_formed_attribute_input` lint detects ill-formed attribute
/// inputs that were previously accepted and used in practice.
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ mir_build_extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
.note = extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
.label = use of extern static
mir_build_feature_detection = Rust language and library feature detection via `version_check::feature_enabled` does not actually check whether the feature works correctly
.note = the fucntion has been disabled and will always return `None` on a nightly compiler.
mir_build_indirect_structural_match =
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq)]`
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::build::expr::as_place::PlaceBuilder;
use crate::build::scope::DropKind;
use crate::errors::FeatureDetection;
use hir::def_id::LOCAL_CRATE;
use itertools::Itertools;
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
Expand All @@ -21,6 +22,7 @@ use rustc_middle::mir::*;
use rustc_middle::query::TyCtxtAt;
use rustc_middle::thir::{self, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
use rustc_session::lint;
use rustc_span::symbol::sym;
use rustc_span::Symbol;
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -535,6 +537,14 @@ fn construct_fn<'tcx>(
),
);
builder.cfg.terminate(block, source_info, TerminatorKind::Return);

tcx.emit_node_span_lint(
lint::builtin::FEATURE_DETECTION,
fn_id,
span,
FeatureDetection { span },
);

return builder.finish();
}

Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,3 +950,10 @@ pub enum RustcBoxAttrReason {
#[note(mir_build_missing_box)]
MissingBox,
}

#[derive(LintDiagnostic)]
#[diag(mir_build_feature_detection)]
pub struct FeatureDetection {
#[note]
pub span: Span,
}
2 changes: 2 additions & 0 deletions tests/ui/feature-gates/version_check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//@ run-pass

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

Expand Down
17 changes: 17 additions & 0 deletions tests/ui/feature-gates/version_check.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
warning: Rust language and library feature detection via `version_check::feature_enabled` does not actually check whether the feature works correctly
--> $DIR/version_check.rs:3:1
|
LL | fn supports_feature(_: &str) -> Option<bool> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: nightly feature detection is disabled
= note: for more information, see TODO
note: the fucntion has been disabled and will always return `None` on a nightly compiler.
--> $DIR/version_check.rs:3:1
|
LL | fn supports_feature(_: &str) -> Option<bool> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `#[warn(feature_detection)]` on by default

warning: 1 warning emitted

0 comments on commit ffbf62c

Please sign in to comment.