Check for MSRV attributes in late passes using the HIR

This commit is contained in:
Alex Macleod
2024-12-13 17:40:07 +00:00
parent 2cdb90d961
commit 0972c3b565
12 changed files with 246 additions and 214 deletions

View File

@@ -137,24 +137,13 @@ use rustc_middle::hir::nested_filter;
#[macro_export]
macro_rules! extract_msrv_attr {
(LateContext) => {
fn check_attributes(&mut self, cx: &rustc_lint::LateContext<'_>, attrs: &[rustc_hir::Attribute]) {
() => {
fn check_attributes(&mut self, cx: &rustc_lint::EarlyContext<'_>, attrs: &[rustc_ast::ast::Attribute]) {
let sess = rustc_lint::LintContext::sess(cx);
self.msrv.check_attributes(sess, attrs);
}
fn check_attributes_post(&mut self, cx: &rustc_lint::LateContext<'_>, attrs: &[rustc_hir::Attribute]) {
let sess = rustc_lint::LintContext::sess(cx);
self.msrv.check_attributes_post(sess, attrs);
}
};
(EarlyContext) => {
fn check_attributes(&mut self, cx: &rustc_lint::EarlyContext<'_>, attrs: &[rustc_ast::Attribute]) {
let sess = rustc_lint::LintContext::sess(cx);
self.msrv.check_attributes(sess, attrs);
}
fn check_attributes_post(&mut self, cx: &rustc_lint::EarlyContext<'_>, attrs: &[rustc_ast::Attribute]) {
fn check_attributes_post(&mut self, cx: &rustc_lint::EarlyContext<'_>, attrs: &[rustc_ast::ast::Attribute]) {
let sess = rustc_lint::LintContext::sess(cx);
self.msrv.check_attributes_post(sess, attrs);
}

View File

@@ -1,11 +1,14 @@
use rustc_ast::Attribute;
use rustc_ast::attr::AttributeExt;
use rustc_attr_parsing::{RustcVersion, parse_version};
use rustc_lint::LateContext;
use rustc_session::Session;
use rustc_span::{Symbol, sym};
use serde::Deserialize;
use smallvec::{SmallVec, smallvec};
use std::fmt;
use smallvec::SmallVec;
use std::iter::once;
use std::sync::atomic::{AtomicBool, Ordering};
macro_rules! msrv_aliases {
($($major:literal,$minor:literal,$patch:literal {
@@ -73,21 +76,15 @@ msrv_aliases! {
1,15,0 { MAYBE_BOUND_IN_WHERE }
}
/// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]`
#[derive(Debug, Clone)]
pub struct Msrv {
stack: SmallVec<[RustcVersion; 2]>,
}
/// `#[clippy::msrv]` attributes are rarely used outside of Clippy's test suite, as a basic
/// optimization we can skip traversing the HIR in [`Msrv::meets`] if we never saw an MSRV attribute
/// during the early lint passes
static SEEN_MSRV_ATTR: AtomicBool = AtomicBool::new(false);
impl fmt::Display for Msrv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(msrv) = self.current() {
write!(f, "{msrv}")
} else {
f.write_str("1.0.0")
}
}
}
/// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]` in late
/// lint passes, use [`MsrvStack`] for early passes
#[derive(Copy, Clone, Debug, Default)]
pub struct Msrv(Option<RustcVersion>);
impl<'de> Deserialize<'de> for Msrv {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -96,14 +93,36 @@ impl<'de> Deserialize<'de> for Msrv {
{
let v = String::deserialize(deserializer)?;
parse_version(Symbol::intern(&v))
.map(|v| Msrv { stack: smallvec![v] })
.map(|v| Self(Some(v)))
.ok_or_else(|| serde::de::Error::custom("not a valid Rust version"))
}
}
impl Msrv {
pub fn empty() -> Msrv {
Msrv { stack: SmallVec::new() }
/// Returns the MSRV at the current node
///
/// If the crate being linted uses an `#[clippy::msrv]` attribute this will search the parent
/// nodes for that attribute, prefer to run this check after cheaper pattern matching operations
pub fn current(self, cx: &LateContext<'_>) -> Option<RustcVersion> {
if SEEN_MSRV_ATTR.load(Ordering::Relaxed) {
let start = cx.last_node_with_lint_attrs;
if let Some(msrv_attr) = once(start)
.chain(cx.tcx.hir_parent_id_iter(start))
.find_map(|id| parse_attrs(cx.tcx.sess, cx.tcx.hir().attrs(id)))
{
return Some(msrv_attr);
}
}
self.0
}
/// Checks if a required version from [this module](self) is met at the current node
///
/// If the crate being linted uses an `#[clippy::msrv]` attribute this will search the parent
/// nodes for that attribute, prefer to run this check after cheaper pattern matching operations
pub fn meets(self, cx: &LateContext<'_>, required: RustcVersion) -> bool {
self.current(cx).is_none_or(|msrv| msrv >= required)
}
pub fn read_cargo(&mut self, sess: &Session) {
@@ -111,8 +130,8 @@ impl Msrv {
.ok()
.and_then(|v| parse_version(Symbol::intern(&v)));
match (self.current(), cargo_msrv) {
(None, Some(cargo_msrv)) => self.stack = smallvec![cargo_msrv],
match (self.0, cargo_msrv) {
(None, Some(cargo_msrv)) => self.0 = Some(cargo_msrv),
(Some(clippy_msrv), Some(cargo_msrv)) => {
if clippy_msrv != cargo_msrv {
sess.dcx().warn(format!(
@@ -123,6 +142,21 @@ impl Msrv {
_ => {},
}
}
}
/// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]` in early
/// lint passes, use [`Msrv`] for late passes
#[derive(Debug, Clone)]
pub struct MsrvStack {
stack: SmallVec<[RustcVersion; 2]>,
}
impl MsrvStack {
pub fn new(initial: Msrv) -> Self {
Self {
stack: SmallVec::from_iter(initial.0),
}
}
pub fn current(&self) -> Option<RustcVersion> {
self.stack.last().copied()
@@ -132,42 +166,43 @@ impl Msrv {
self.current().is_none_or(|msrv| msrv >= required)
}
fn parse_attr(sess: &Session, attrs: &[impl AttributeExt]) -> Option<RustcVersion> {
let sym_msrv = Symbol::intern("msrv");
let mut msrv_attrs = attrs.iter().filter(|attr| attr.path_matches(&[sym::clippy, sym_msrv]));
if let Some(msrv_attr) = msrv_attrs.next() {
if let Some(duplicate) = msrv_attrs.next_back() {
sess.dcx()
.struct_span_err(duplicate.span(), "`clippy::msrv` is defined multiple times")
.with_span_note(msrv_attr.span(), "first definition found here")
.emit();
}
if let Some(msrv) = msrv_attr.value_str() {
if let Some(version) = parse_version(msrv) {
return Some(version);
}
sess.dcx()
.span_err(msrv_attr.span(), format!("`{msrv}` is not a valid Rust version"));
} else {
sess.dcx().span_err(msrv_attr.span(), "bad clippy attribute");
}
}
None
}
pub fn check_attributes(&mut self, sess: &Session, attrs: &[impl AttributeExt]) {
if let Some(version) = Self::parse_attr(sess, attrs) {
pub fn check_attributes(&mut self, sess: &Session, attrs: &[Attribute]) {
if let Some(version) = parse_attrs(sess, attrs) {
SEEN_MSRV_ATTR.store(true, Ordering::Relaxed);
self.stack.push(version);
}
}
pub fn check_attributes_post(&mut self, sess: &Session, attrs: &[impl AttributeExt]) {
if Self::parse_attr(sess, attrs).is_some() {
pub fn check_attributes_post(&mut self, sess: &Session, attrs: &[Attribute]) {
if parse_attrs(sess, attrs).is_some() {
self.stack.pop();
}
}
}
fn parse_attrs(sess: &Session, attrs: &[impl AttributeExt]) -> Option<RustcVersion> {
let sym_msrv = Symbol::intern("msrv");
let mut msrv_attrs = attrs.iter().filter(|attr| attr.path_matches(&[sym::clippy, sym_msrv]));
if let Some(msrv_attr) = msrv_attrs.next() {
if let Some(duplicate) = msrv_attrs.next_back() {
sess.dcx()
.struct_span_err(duplicate.span(), "`clippy::msrv` is defined multiple times")
.with_span_note(msrv_attr.span(), "first definition found here")
.emit();
}
if let Some(msrv) = msrv_attr.value_str() {
if let Some(version) = parse_version(msrv) {
return Some(version);
}
sess.dcx()
.span_err(msrv_attr.span(), format!("`{msrv}` is not a valid Rust version"));
} else {
sess.dcx().span_err(msrv_attr.span(), "bad clippy attribute");
}
}
None
}

View File

@@ -19,7 +19,6 @@ pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];
pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"];
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"];
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
pub const SYMBOL: [&str; 3] = ["rustc_span", "symbol", "Symbol"];
pub const SYMBOL_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Symbol", "as_str"];
@@ -33,7 +32,7 @@ pub const IO_ERROR_NEW: [&str; 5] = ["std", "io", "error", "Error", "new"];
pub const IO_ERRORKIND_OTHER: [&str; 5] = ["std", "io", "error", "ErrorKind", "Other"];
// Paths in clippy itself
pub const MSRV: [&str; 3] = ["clippy_utils", "msrvs", "Msrv"];
pub const MSRV_STACK: [&str; 3] = ["clippy_utils", "msrvs", "MsrvStack"];
// Paths in external crates
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates

View File

@@ -11,6 +11,7 @@ use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::Obligation;
use rustc_lint::LateContext;
use rustc_middle::mir::{
Body, CastKind, NonDivergingIntrinsic, NullOp, Operand, Place, ProjectionElem, Rvalue, Statement, StatementKind,
Terminator, TerminatorKind,
@@ -25,16 +26,16 @@ use std::borrow::Cow;
type McfResult = Result<(), (Span, Cow<'static, str>)>;
pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv) -> McfResult {
pub fn is_min_const_fn<'tcx>(cx: &LateContext<'tcx>, body: &Body<'tcx>, msrv: Msrv) -> McfResult {
let def_id = body.source.def_id();
for local in &body.local_decls {
check_ty(tcx, local.ty, local.source_info.span, msrv)?;
check_ty(cx, local.ty, local.source_info.span, msrv)?;
}
// impl trait is gone in MIR, so check the return type manually
check_ty(
tcx,
tcx.fn_sig(def_id).instantiate_identity().output().skip_binder(),
cx,
cx.tcx.fn_sig(def_id).instantiate_identity().output().skip_binder(),
body.local_decls.iter().next().unwrap().source_info.span,
msrv,
)?;
@@ -43,16 +44,16 @@ pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv)
// Cleanup blocks are ignored entirely by const eval, so we can too:
// https://github.com/rust-lang/rust/blob/1dea922ea6e74f99a0e97de5cdb8174e4dea0444/compiler/rustc_const_eval/src/transform/check_consts/check.rs#L382
if !bb.is_cleanup {
check_terminator(tcx, body, bb.terminator(), msrv)?;
check_terminator(cx, body, bb.terminator(), msrv)?;
for stmt in &bb.statements {
check_statement(tcx, body, def_id, stmt, msrv)?;
check_statement(cx, body, def_id, stmt, msrv)?;
}
}
}
Ok(())
}
fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, msrv: &Msrv) -> McfResult {
fn check_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, msrv: Msrv) -> McfResult {
for arg in ty.walk() {
let ty = match arg.unpack() {
GenericArgKind::Type(ty) => ty,
@@ -63,7 +64,7 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, msrv: &Msrv) -> M
};
match ty.kind() {
ty::Ref(_, _, hir::Mutability::Mut) if !msrv.meets(msrvs::CONST_MUT_REFS) => {
ty::Ref(_, _, hir::Mutability::Mut) if !msrv.meets(cx, msrvs::CONST_MUT_REFS) => {
return Err((span, "mutable references in const fn are unstable".into()));
},
ty::Alias(ty::Opaque, ..) => return Err((span, "`impl Trait` in const fn is unstable".into())),
@@ -82,7 +83,7 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, msrv: &Msrv) -> M
));
},
ty::ExistentialPredicate::Trait(trait_ref) => {
if Some(trait_ref.def_id) != tcx.lang_items().sized_trait() {
if Some(trait_ref.def_id) != cx.tcx.lang_items().sized_trait() {
return Err((
span,
"trait bounds other than `Sized` \
@@ -101,19 +102,19 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, msrv: &Msrv) -> M
}
fn check_rvalue<'tcx>(
tcx: TyCtxt<'tcx>,
cx: &LateContext<'tcx>,
body: &Body<'tcx>,
def_id: DefId,
rvalue: &Rvalue<'tcx>,
span: Span,
msrv: &Msrv,
msrv: Msrv,
) -> McfResult {
match rvalue {
Rvalue::ThreadLocalRef(_) => Err((span, "cannot access thread local storage in const fn".into())),
Rvalue::Len(place) | Rvalue::Discriminant(place) | Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => {
check_place(tcx, *place, span, body, msrv)
check_place(cx, *place, span, body, msrv)
},
Rvalue::CopyForDeref(place) => check_place(tcx, *place, span, body, msrv),
Rvalue::CopyForDeref(place) => check_place(cx, *place, span, body, msrv),
Rvalue::Repeat(operand, _)
| Rvalue::Use(operand)
| Rvalue::WrapUnsafeBinder(operand, _)
@@ -128,7 +129,7 @@ fn check_rvalue<'tcx>(
| CastKind::PointerCoercion(PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer, _),
operand,
_,
) => check_operand(tcx, operand, span, body, msrv),
) => check_operand(cx, operand, span, body, msrv),
Rvalue::Cast(
CastKind::PointerCoercion(
PointerCoercion::UnsafeFnPointer
@@ -144,9 +145,11 @@ fn check_rvalue<'tcx>(
// We cannot allow this for now.
return Err((span, "unsizing casts are only allowed for references right now".into()));
};
let unsized_ty = tcx.struct_tail_for_codegen(pointee_ty, ty::TypingEnv::post_analysis(tcx, def_id));
let unsized_ty = cx
.tcx
.struct_tail_for_codegen(pointee_ty, ty::TypingEnv::post_analysis(cx.tcx, def_id));
if let ty::Slice(_) | ty::Str = unsized_ty.kind() {
check_operand(tcx, op, span, body, msrv)?;
check_operand(cx, op, span, body, msrv)?;
// Casting/coercing things to slices is fine.
Ok(())
} else {
@@ -167,9 +170,9 @@ fn check_rvalue<'tcx>(
)),
// binops are fine on integers
Rvalue::BinaryOp(_, box (lhs, rhs)) => {
check_operand(tcx, lhs, span, body, msrv)?;
check_operand(tcx, rhs, span, body, msrv)?;
let ty = lhs.ty(body, tcx);
check_operand(cx, lhs, span, body, msrv)?;
check_operand(cx, rhs, span, body, msrv)?;
let ty = lhs.ty(body, cx.tcx);
if ty.is_integral() || ty.is_bool() || ty.is_char() {
Ok(())
} else {
@@ -185,16 +188,16 @@ fn check_rvalue<'tcx>(
)
| Rvalue::ShallowInitBox(_, _) => Ok(()),
Rvalue::UnaryOp(_, operand) => {
let ty = operand.ty(body, tcx);
let ty = operand.ty(body, cx.tcx);
if ty.is_integral() || ty.is_bool() {
check_operand(tcx, operand, span, body, msrv)
check_operand(cx, operand, span, body, msrv)
} else {
Err((span, "only int and `bool` operations are stable in const fn".into()))
}
},
Rvalue::Aggregate(_, operands) => {
for operand in operands {
check_operand(tcx, operand, span, body, msrv)?;
check_operand(cx, operand, span, body, msrv)?;
}
Ok(())
},
@@ -202,33 +205,33 @@ fn check_rvalue<'tcx>(
}
fn check_statement<'tcx>(
tcx: TyCtxt<'tcx>,
cx: &LateContext<'tcx>,
body: &Body<'tcx>,
def_id: DefId,
statement: &Statement<'tcx>,
msrv: &Msrv,
msrv: Msrv,
) -> McfResult {
let span = statement.source_info.span;
match &statement.kind {
StatementKind::Assign(box (place, rval)) => {
check_place(tcx, *place, span, body, msrv)?;
check_rvalue(tcx, body, def_id, rval, span, msrv)
check_place(cx, *place, span, body, msrv)?;
check_rvalue(cx, body, def_id, rval, span, msrv)
},
StatementKind::FakeRead(box (_, place)) => check_place(tcx, *place, span, body, msrv),
StatementKind::FakeRead(box (_, place)) => check_place(cx, *place, span, body, msrv),
// just an assignment
StatementKind::SetDiscriminant { place, .. } | StatementKind::Deinit(place) => {
check_place(tcx, **place, span, body, msrv)
check_place(cx, **place, span, body, msrv)
},
StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => check_operand(tcx, op, span, body, msrv),
StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => check_operand(cx, op, span, body, msrv),
StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
rustc_middle::mir::CopyNonOverlapping { dst, src, count },
)) => {
check_operand(tcx, dst, span, body, msrv)?;
check_operand(tcx, src, span, body, msrv)?;
check_operand(tcx, count, span, body, msrv)
check_operand(cx, dst, span, body, msrv)?;
check_operand(cx, src, span, body, msrv)?;
check_operand(cx, count, span, body, msrv)
},
// These are all NOPs
StatementKind::StorageLive(_)
@@ -244,16 +247,16 @@ fn check_statement<'tcx>(
}
fn check_operand<'tcx>(
tcx: TyCtxt<'tcx>,
cx: &LateContext<'tcx>,
operand: &Operand<'tcx>,
span: Span,
body: &Body<'tcx>,
msrv: &Msrv,
msrv: Msrv,
) -> McfResult {
match operand {
Operand::Move(place) => {
if !place.projection.as_ref().is_empty()
&& !is_ty_const_destruct(tcx, place.ty(&body.local_decls, tcx).ty, body)
&& !is_ty_const_destruct(cx.tcx, place.ty(&body.local_decls, cx.tcx).ty, body)
{
return Err((
span,
@@ -261,29 +264,35 @@ fn check_operand<'tcx>(
));
}
check_place(tcx, *place, span, body, msrv)
check_place(cx, *place, span, body, msrv)
},
Operand::Copy(place) => check_place(tcx, *place, span, body, msrv),
Operand::Constant(c) => match c.check_static_ptr(tcx) {
Operand::Copy(place) => check_place(cx, *place, span, body, msrv),
Operand::Constant(c) => match c.check_static_ptr(cx.tcx) {
Some(_) => Err((span, "cannot access `static` items in const fn".into())),
None => Ok(()),
},
}
}
fn check_place<'tcx>(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &Body<'tcx>, msrv: &Msrv) -> McfResult {
fn check_place<'tcx>(
cx: &LateContext<'tcx>,
place: Place<'tcx>,
span: Span,
body: &Body<'tcx>,
msrv: Msrv,
) -> McfResult {
for (base, elem) in place.as_ref().iter_projections() {
match elem {
ProjectionElem::Field(..) => {
if base.ty(body, tcx).ty.is_union() && !msrv.meets(msrvs::CONST_FN_UNION) {
if base.ty(body, cx.tcx).ty.is_union() && !msrv.meets(cx, msrvs::CONST_FN_UNION) {
return Err((span, "accessing union fields is unstable".into()));
}
},
ProjectionElem::Deref => match base.ty(body, tcx).ty.kind() {
ProjectionElem::Deref => match base.ty(body, cx.tcx).ty.kind() {
ty::RawPtr(_, hir::Mutability::Mut) => {
return Err((span, "dereferencing raw mut pointer in const fn is unstable".into()));
},
ty::RawPtr(_, hir::Mutability::Not) if !msrv.meets(msrvs::CONST_RAW_PTR_DEREF) => {
ty::RawPtr(_, hir::Mutability::Not) if !msrv.meets(cx, msrvs::CONST_RAW_PTR_DEREF) => {
return Err((span, "dereferencing raw const pointer in const fn is unstable".into()));
},
_ => (),
@@ -302,10 +311,10 @@ fn check_place<'tcx>(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &B
}
fn check_terminator<'tcx>(
tcx: TyCtxt<'tcx>,
cx: &LateContext<'tcx>,
body: &Body<'tcx>,
terminator: &Terminator<'tcx>,
msrv: &Msrv,
msrv: Msrv,
) -> McfResult {
let span = terminator.source_info.span;
match &terminator.kind {
@@ -317,7 +326,7 @@ fn check_terminator<'tcx>(
| TerminatorKind::UnwindTerminate(_)
| TerminatorKind::Unreachable => Ok(()),
TerminatorKind::Drop { place, .. } => {
if !is_ty_const_destruct(tcx, place.ty(&body.local_decls, tcx).ty, body) {
if !is_ty_const_destruct(cx.tcx, place.ty(&body.local_decls, cx.tcx).ty, body) {
return Err((
span,
"cannot drop locals with a non constant destructor in const fn".into(),
@@ -325,7 +334,7 @@ fn check_terminator<'tcx>(
}
Ok(())
},
TerminatorKind::SwitchInt { discr, targets: _ } => check_operand(tcx, discr, span, body, msrv),
TerminatorKind::SwitchInt { discr, targets: _ } => check_operand(cx, discr, span, body, msrv),
TerminatorKind::CoroutineDrop | TerminatorKind::Yield { .. } => {
Err((span, "const fn coroutines are unstable".into()))
},
@@ -339,9 +348,9 @@ fn check_terminator<'tcx>(
fn_span: _,
}
| TerminatorKind::TailCall { func, args, fn_span: _ } => {
let fn_ty = func.ty(body, tcx);
let fn_ty = func.ty(body, cx.tcx);
if let ty::FnDef(fn_def_id, _) = *fn_ty.kind() {
if !is_stable_const_fn(tcx, fn_def_id, msrv) {
if !is_stable_const_fn(cx, fn_def_id, msrv) {
return Err((
span,
format!(
@@ -356,17 +365,17 @@ fn check_terminator<'tcx>(
// within const fns. `transmute` is allowed in all other const contexts.
// This won't really scale to more intrinsics or functions. Let's allow const
// transmutes in const fn before we add more hacks to this.
if tcx.is_intrinsic(fn_def_id, sym::transmute) {
if cx.tcx.is_intrinsic(fn_def_id, sym::transmute) {
return Err((
span,
"can only call `transmute` from const items, not `const fn`".into(),
));
}
check_operand(tcx, func, span, body, msrv)?;
check_operand(cx, func, span, body, msrv)?;
for arg in args {
check_operand(tcx, &arg.node, span, body, msrv)?;
check_operand(cx, &arg.node, span, body, msrv)?;
}
Ok(())
} else {
@@ -379,14 +388,14 @@ fn check_terminator<'tcx>(
msg: _,
target: _,
unwind: _,
} => check_operand(tcx, cond, span, body, msrv),
} => check_operand(cx, cond, span, body, msrv),
TerminatorKind::InlineAsm { .. } => Err((span, "cannot use inline assembly in const fn".into())),
}
}
fn is_stable_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool {
tcx.is_const_fn(def_id)
&& tcx.lookup_const_stability(def_id).is_none_or(|const_stab| {
fn is_stable_const_fn(cx: &LateContext<'_>, def_id: DefId, msrv: Msrv) -> bool {
cx.tcx.is_const_fn(def_id)
&& cx.tcx.lookup_const_stability(def_id).is_none_or(|const_stab| {
if let rustc_attr_parsing::StabilityLevel::Stable { since, .. } = const_stab.level {
// Checking MSRV is manually necessary because `rustc` has no such concept. This entire
// function could be removed if `rustc` provided a MSRV-aware version of `is_stable_const_fn`.
@@ -398,10 +407,10 @@ fn is_stable_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool {
StableSince::Err => return false,
};
msrv.meets(const_stab_rust_version)
msrv.meets(cx, const_stab_rust_version)
} else {
// Unstable const fn, check if the feature is enabled.
tcx.features().enabled(const_stab.feature) && msrv.current().is_none()
cx.tcx.features().enabled(const_stab.feature) && msrv.current(cx).is_none()
}
})
}