Skip to content
Merged
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
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ fn explain_lint_level_source(
/// If you are looking to implement a lint, look for higher level functions,
/// for example:
/// - [`TyCtxt::emit_node_span_lint`]
/// - [`TyCtxt::node_span_lint`]
/// - [`TyCtxt::node_lint`]
/// - `LintContext::opt_span_lint`
///
Expand Down Expand Up @@ -488,7 +487,6 @@ pub fn lint_level(
/// for example:
///
/// - [`TyCtxt::emit_node_span_lint`]
/// - [`TyCtxt::node_span_lint`]
/// - [`TyCtxt::node_lint`]
/// - `LintContext::opt_span_lint`
///
Expand Down
15 changes: 0 additions & 15 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2542,21 +2542,6 @@ impl<'tcx> TyCtxt<'tcx> {
diag_lint_level(self.sess, lint, level, Some(span.into()), decorator)
}

/// Emit a lint at the appropriate level for a hir node, with an associated span.
///
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
#[track_caller]
pub fn node_span_lint(
self,
lint: &'static Lint,
hir_id: HirId,
span: impl Into<MultiSpan>,
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
) {
let level = self.lint_level_at_node(lint, hir_id);
lint_level(self.sess, lint, level, Some(span.into()), decorate);
}

/// Find the appropriate span where `use` and outer attributes can be inserted at.
pub fn crate_level_attribute_injection_span(self) -> Span {
let node = self.hir_node(hir::CRATE_HIR_ID);
Expand Down
21 changes: 18 additions & 3 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ impl<T> Option<T> {
#[rustc_const_unstable(feature = "const_option_ops", issue = "143956")]
pub const fn get_or_insert_default(&mut self) -> &mut T
where
T: [const] Default + [const] Destruct,
T: [const] Default,
{
self.get_or_insert_with(T::default)
}
Expand Down Expand Up @@ -1805,10 +1805,25 @@ impl<T> Option<T> {
pub const fn get_or_insert_with<F>(&mut self, f: F) -> &mut T
where
F: [const] FnOnce() -> T + [const] Destruct,
T: [const] Destruct,
{
if let None = self {
*self = Some(f());
// The effect of the following statement is identical to
// *self = Some(f());
// except that it does not drop the old value of `*self`. This is not a leak, because
// we just checked that the old value is `None`, which contains no fields to drop.
// This implementation strategy
//
// * avoids needing a `T: [const] Destruct` bound, to the benefit of `const` callers,
// * and avoids possibly compiling needless drop code (as would sometimes happen in the
// previous implementation), to the benefit of non-`const` callers.
//
// FIXME(const-hack): It would be nice if this weird trick were made obsolete
// (though that is likely to be hard/wontfix).
//
// It could also be expressed as `unsafe { core::ptr::write(self, Some(f())) }`, but
// no reason is currently known to use additional unsafe code here.

mem::forget(mem::replace(self, Some(f())));
}

// SAFETY: a `None` variant for `self` would have been replaced by a `Some`
Expand Down
24 changes: 24 additions & 0 deletions library/coretests/tests/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,30 @@ const fn option_const_mut() {
*/
}

/// Test that `Option::get_or_insert_default` is usable in const contexts, including with types that
/// do not satisfy `T: const Destruct`.
#[test]
fn const_get_or_insert_default() {
const OPT_DEFAULT: Option<Vec<bool>> = {
let mut x = None;
x.get_or_insert_default();
x
};
assert!(OPT_DEFAULT.is_some());
}

/// Test that `Option::get_or_insert_with` is usable in const contexts, including with types that
/// do not satisfy `T: const Destruct`.
#[test]
fn const_get_or_insert_with() {
const OPT_WITH: Option<Vec<bool>> = {
let mut x = None;
x.get_or_insert_with(Vec::new);
x
};
assert!(OPT_WITH.is_some());
}

#[test]
fn test_unwrap_drop() {
struct Dtor<'a> {
Expand Down
53 changes: 29 additions & 24 deletions src/librustdoc/passes/lint/bare_urls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::mem;
use std::sync::LazyLock;

use regex::Regex;
use rustc_errors::Applicability;
use rustc_errors::{Applicability, DiagDecorator};
use rustc_hir::HirId;
use rustc_resolve::rustdoc::pulldown_cmark::{Event, Parser, Tag};
use rustc_resolve::rustdoc::source_span_for_markdown_range;
Expand All @@ -24,30 +24,35 @@ pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &
let maybe_sp = source_span_for_markdown_range(cx.tcx, dox, &range, &item.attrs.doc_strings)
.map(|(sp, _)| sp);
let sp = maybe_sp.unwrap_or_else(|| item.attr_span(cx.tcx));
cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
lint.primary_message(msg)
.note("bare URLs are not automatically turned into clickable links");
// The fallback of using the attribute span is suitable for
// highlighting where the error is, but not for placing the < and >
if let Some(sp) = maybe_sp {
if let Some(without_brackets) = without_brackets {
lint.multipart_suggestion(
"use an automatic link instead",
vec![(sp, format!("<{without_brackets}>"))],
Applicability::MachineApplicable,
);
} else {
lint.multipart_suggestion(
"use an automatic link instead",
vec![
(sp.shrink_to_lo(), "<".to_string()),
(sp.shrink_to_hi(), ">".to_string()),
],
Applicability::MachineApplicable,
);
cx.tcx.emit_node_span_lint(
crate::lint::BARE_URLS,
hir_id,
sp,
DiagDecorator(|lint| {
lint.primary_message(msg)
.note("bare URLs are not automatically turned into clickable links");
// The fallback of using the attribute span is suitable for
// highlighting where the error is, but not for placing the < and >
if let Some(sp) = maybe_sp {
if let Some(without_brackets) = without_brackets {
lint.multipart_suggestion(
"use an automatic link instead",
vec![(sp, format!("<{without_brackets}>"))],
Applicability::MachineApplicable,
);
} else {
lint.multipart_suggestion(
"use an automatic link instead",
vec![
(sp.shrink_to_lo(), "<".to_string()),
(sp.shrink_to_hi(), ">".to_string()),
],
Applicability::MachineApplicable,
);
}
}
}
});
}),
);
};

let mut p = Parser::new_ext(dox, main_body_opts()).into_offset_iter();
Expand Down
8 changes: 7 additions & 1 deletion src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,13 @@ pub enum ItemEnum {
}

impl ItemEnum {
/// Returns the [`ItemKind`] of this item.
/// Get just the kind of this item, but with no further data.
///
/// ```rust
/// # use rustdoc_json_types::{ItemKind, ItemEnum};
/// let item = ItemEnum::ExternCrate { name: "libc".to_owned(), rename: None };
/// assert_eq!(item.item_kind(), ItemKind::ExternCrate);
/// ```
pub fn item_kind(&self) -> ItemKind {
match self {
ItemEnum::Module(_) => ItemKind::Module,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ path = "rustc_lint::context::LintContext::span_lint"
reason = "this function does not add a link to our documentation; please use the `clippy_utils::diagnostics::span_lint*` functions instead"

[[disallowed-methods]]
path = "rustc_middle::ty::context::TyCtxt::node_span_lint"
path = "rustc_middle::ty::context::TyCtxt::emit_node_span_lint"
reason = "this function does not add a link to our documentation; please use the `clippy_utils::diagnostics::span_lint_hir*` functions instead"
4 changes: 2 additions & 2 deletions src/tools/clippy/clippy_utils/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ pub fn span_lint_hir_and_then(
f: impl FnOnce(&mut Diag<'_, ()>),
) {
#[expect(clippy::disallowed_methods)]
cx.tcx.node_span_lint(lint, hir_id, sp, |diag| {
cx.tcx.emit_node_span_lint(lint, hir_id, sp, rustc_errors::DiagDecorator(|diag| {
diag.primary_message(msg);
f(diag);
docs_link(diag, lint);

#[cfg(debug_assertions)]
validate_diag(diag);
});
}));
}

/// Add a span lint with a suggestion on how to fix it.
Expand Down
6 changes: 3 additions & 3 deletions src/tools/clippy/tests/ui-internal/disallow_span_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern crate rustc_hir;
extern crate rustc_lint;
extern crate rustc_middle;

use rustc_errors::{DiagMessage, MultiSpan};
use rustc_errors::{DiagDecorator, DiagMessage, MultiSpan};
use rustc_hir::hir_id::HirId;
use rustc_lint::{Lint, LintContext};
use rustc_middle::ty::TyCtxt;
Expand All @@ -19,10 +19,10 @@ pub fn a(cx: impl LintContext, lint: &'static Lint, span: impl Into<MultiSpan>,
}

pub fn b(tcx: TyCtxt<'_>, lint: &'static Lint, hir_id: HirId, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) {
tcx.node_span_lint(lint, hir_id, span, |lint| {
tcx.emit_node_span_lint(lint, hir_id, span, DiagDecorator(|lint| {
//~^ disallowed_methods
lint.primary_message(msg);
});
}));
}

fn main() {}
1 change: 1 addition & 0 deletions tests/ui/cfg/both-true-false.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// Test that placing a `cfg(true)` and `cfg(false)` on the same item result in
//. it being disabled.`
//@ reference: cfg.attr.duplicates

#[cfg(false)]
#[cfg(true)]
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/cfg/both-true-false.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
error[E0425]: cannot find function `foo` in this scope
--> $DIR/both-true-false.rs:13:5
--> $DIR/both-true-false.rs:14:5
|
LL | foo();
| ^^^ not found in this scope
|
note: found an item that was configured out
--> $DIR/both-true-false.rs:6:4
--> $DIR/both-true-false.rs:7:4
|
LL | #[cfg(false)]
| ----- the item is gated here
LL | #[cfg(true)]
LL | fn foo() {}
| ^^^
note: found an item that was configured out
--> $DIR/both-true-false.rs:10:4
--> $DIR/both-true-false.rs:11:4
|
LL | #[cfg(false)]
| ----- the item is gated here
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cfg/cfg-false-use-item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Test that use items with cfg(false) are properly filtered out

//@ run-pass
//@ reference: cfg.predicate.literal
//@ reference: cfg.attr.effect

pub fn main() {
// Make sure that this view item is filtered out because otherwise it would
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cfg/cfg-family.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//@ build-pass
//@ ignore-wasm32 no bare family
//@ ignore-sgx
//@ reference: cfg.target_family.unix
//@ reference: cfg.target_family.windows

#[cfg(windows)]
pub fn main() {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cfg/cfg-panic-abort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//@ compile-flags: -C panic=abort
//@ no-prefer-dynamic
//@ ignore-backends: gcc
//@ reference: cfg.panic.def
//@ reference: cfg.panic.values

#[cfg(panic = "unwind")]
pub fn bad() -> i32 { }
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cfg/cfg-panic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//@ build-pass
//@ compile-flags: -C panic=unwind
//@ needs-unwind
//@ reference: cfg.panic.def
//@ reference: cfg.panic.values

#[cfg(panic = "abort")]
pub fn bad() -> i32 { }
Expand Down
1 change: 1 addition & 0 deletions tests/ui/cfg/cfg-path-error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ check-fail
//@ reference: cfg.option-spec

#![allow(unexpected_cfgs)] // invalid cfgs

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/cfg/cfg-path-error.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0539]: malformed `cfg` attribute input
--> $DIR/cfg-path-error.rs:5:1
--> $DIR/cfg-path-error.rs:6:1
|
LL | #[cfg(any(foo, foo::bar))]
| ^^^^^^^^^^^^^^^--------^^^
Expand All @@ -10,7 +10,7 @@ LL | #[cfg(any(foo, foo::bar))]
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>

error[E0539]: malformed `cfg` attribute input
--> $DIR/cfg-path-error.rs:11:1
--> $DIR/cfg-path-error.rs:12:1
|
LL | #[cfg(any(foo::bar, foo))]
| ^^^^^^^^^^--------^^^^^^^^
Expand All @@ -21,7 +21,7 @@ LL | #[cfg(any(foo::bar, foo))]
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>

error[E0539]: malformed `cfg` attribute input
--> $DIR/cfg-path-error.rs:17:1
--> $DIR/cfg-path-error.rs:18:1
|
LL | #[cfg(all(foo, foo::bar))]
| ^^^^^^^^^^^^^^^--------^^^
Expand All @@ -32,7 +32,7 @@ LL | #[cfg(all(foo, foo::bar))]
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>

error[E0539]: malformed `cfg` attribute input
--> $DIR/cfg-path-error.rs:23:1
--> $DIR/cfg-path-error.rs:24:1
|
LL | #[cfg(all(foo::bar, foo))]
| ^^^^^^^^^^--------^^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cfg/cfg-target-abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ run-pass
//@ reference: cfg.target_abi.def
//@ reference: cfg.target_abi.values

#[cfg(target_abi = "eabihf")]
pub fn main() {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cfg/cfg-target-family.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ build-pass
//@ ignore-sgx
//@ reference: cfg.target_family.def
//@ reference: cfg.target_family.values


#[cfg(target_family = "windows")]
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cfg/cfg-target-vendor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ run-pass
//@ reference: cfg.target_vendor.def
//@ reference: cfg.target_vendor.values
#[cfg(target_vendor = "unknown")]
pub fn main() {
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cfg/cfg_attr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-pass
//@ compile-flags:--cfg set1 --cfg set2
//@ reference: cfg.cfg_attr.intro
//@ reference: cfg.cfg_attr.syntax

#![allow(dead_code, unexpected_cfgs)]

Expand Down
1 change: 1 addition & 0 deletions tests/ui/cfg/cfg_false_no_std-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

//@ check-pass
//@ aux-build: cfg_false_lib_no_std_after.rs
//@ reference: cfg.attr.crate-level-attrs

#![no_std]

Expand Down
1 change: 1 addition & 0 deletions tests/ui/cfg/cfg_false_no_std-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//@ compile-flags: -Cpanic=abort

//@ aux-build: cfg_false_lib_no_std_before.rs
//@ reference: cfg.attr.crate-level-attrs

#![no_std]

Expand Down
1 change: 1 addition & 0 deletions tests/ui/cfg/cfg_false_no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

//@ check-pass
//@ aux-build: cfg_false_lib.rs
//@ reference: cfg.attr.crate-level-attrs

#![no_std]

Expand Down
Loading
Loading