Skip to content

Commit ba14a83

Browse files
committed
Auto merge of #94802 - matthiaskrgr:rollup-4plu0fi, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #92150 (Improve suggestion when casting usize to (possibly) wide pointer) - #94635 (Merge `#[deprecated]` and `#[rustc_deprecated]`) - #94657 (Constify `Index{,Mut}` for `[T]`, `str`, and `[T; N]`) - #94746 (diagnostics: use rustc_on_unimplemented to recommend `[].iter()`) - #94788 (Account for suggestions for complete removal of lines) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 282778a + 6bbaca7 commit ba14a83

File tree

73 files changed

+854
-617
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+854
-617
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

-7
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
437437
)
438438
.emit();
439439
}
440-
} else {
441-
if attr.has_name(sym::deprecated) {
442-
self.sess
443-
.struct_span_err(attr.span, "`#[deprecated]` cannot be used in staged API")
444-
.span_label(attr.span, "use `#[rustc_deprecated]` instead")
445-
.emit();
446-
}
447440
}
448441
}
449442

compiler/rustc_attr/src/builtin.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ where
664664
{
665665
let mut depr: Option<(Deprecation, Span)> = None;
666666
let diagnostic = &sess.parse_sess.span_diagnostic;
667+
let is_rustc = sess.features_untracked().staged_api;
667668

668669
'outer: for attr in attrs_iter {
669670
if !(attr.has_name(sym::deprecated) || attr.has_name(sym::rustc_deprecated)) {
@@ -728,17 +729,31 @@ where
728729
continue 'outer;
729730
}
730731
}
731-
sym::note if attr.has_name(sym::deprecated) => {
732+
sym::note => {
732733
if !get(mi, &mut note) {
733734
continue 'outer;
734735
}
735736
}
737+
// FIXME(jhpratt) remove this after a bootstrap occurs. Emitting an
738+
// error specific to the renaming would be a good idea as well.
736739
sym::reason if attr.has_name(sym::rustc_deprecated) => {
737740
if !get(mi, &mut note) {
738741
continue 'outer;
739742
}
740743
}
741-
sym::suggestion if attr.has_name(sym::rustc_deprecated) => {
744+
sym::suggestion => {
745+
if !sess.features_untracked().deprecated_suggestion {
746+
let mut diag = sess.struct_span_err(
747+
mi.span,
748+
"suggestions on deprecated items are unstable",
749+
);
750+
if sess.is_nightly_build() {
751+
diag.help("add `#![feature(deprecated_suggestion)]` to the crate root");
752+
}
753+
// FIXME(jhpratt) change this to an actual tracking issue
754+
diag.note("see #XXX for more details").emit();
755+
}
756+
742757
if !get(mi, &mut suggestion) {
743758
continue 'outer;
744759
}
@@ -752,7 +767,7 @@ where
752767
if attr.has_name(sym::deprecated) {
753768
&["since", "note"]
754769
} else {
755-
&["since", "reason", "suggestion"]
770+
&["since", "note", "suggestion"]
756771
},
757772
),
758773
);
@@ -775,24 +790,22 @@ where
775790
}
776791
}
777792

778-
if suggestion.is_some() && attr.has_name(sym::deprecated) {
779-
unreachable!("only allowed on rustc_deprecated")
780-
}
781-
782-
if attr.has_name(sym::rustc_deprecated) {
793+
if is_rustc {
783794
if since.is_none() {
784795
handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
785796
continue;
786797
}
787798

788799
if note.is_none() {
789-
struct_span_err!(diagnostic, attr.span, E0543, "missing 'reason'").emit();
800+
struct_span_err!(diagnostic, attr.span, E0543, "missing 'note'").emit();
790801
continue;
791802
}
792803
}
793804

794-
let is_since_rustc_version = attr.has_name(sym::rustc_deprecated);
795-
depr = Some((Deprecation { since, note, suggestion, is_since_rustc_version }, attr.span));
805+
depr = Some((
806+
Deprecation { since, note, suggestion, is_since_rustc_version: is_rustc },
807+
attr.span,
808+
));
796809
}
797810

798811
depr

compiler/rustc_errors/src/emitter.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,31 @@ impl EmitterWriter {
16571657
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
16581658
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
16591659
let mut lines = complete.lines();
1660+
if lines.clone().next().is_none() {
1661+
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
1662+
let line_end = sm.lookup_char_pos(parts[0].span.hi()).line;
1663+
for line in line_start..=line_end {
1664+
buffer.puts(
1665+
row_num - 1 + line - line_start,
1666+
0,
1667+
&self.maybe_anonymized(line),
1668+
Style::LineNumber,
1669+
);
1670+
buffer.puts(
1671+
row_num - 1 + line - line_start,
1672+
max_line_num_len + 1,
1673+
"- ",
1674+
Style::Removal,
1675+
);
1676+
buffer.puts(
1677+
row_num - 1 + line - line_start,
1678+
max_line_num_len + 3,
1679+
&normalize_whitespace(&*file_lines.file.get_line(line - 1).unwrap()),
1680+
Style::Removal,
1681+
);
1682+
}
1683+
row_num += line_end - line_start;
1684+
}
16601685
for (line_pos, (line, highlight_parts)) in
16611686
lines.by_ref().zip(highlights).take(MAX_SUGGESTION_HIGHLIGHT_LINES).enumerate()
16621687
{

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ declare_features! (
362362
(active, default_alloc_error_handler, "1.48.0", Some(66741), None),
363363
/// Allows default type parameters to influence type inference.
364364
(active, default_type_parameter_fallback, "1.3.0", Some(27336), None),
365+
/// Allows having using `suggestion` in the `#[deprecated]` attribute.
366+
(active, deprecated_suggestion, "1.61.0", Some(94785), None),
365367
/// Allows `#[derive(Default)]` and `#[default]` on enums.
366368
(active, derive_default_enum, "1.56.0", Some(86985), None),
367369
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
461461
// DuplicatesOk since it has its own validation
462462
ungated!(
463463
rustc_deprecated, Normal,
464-
template!(List: r#"since = "version", reason = "...""#), DuplicatesOk // See E0550
464+
template!(List: r#"since = "version", note = "...""#), DuplicatesOk // See E0550
465465
),
466466
// DuplicatesOk since it has its own validation
467467
ungated!(

compiler/rustc_middle/src/ty/sty.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,15 @@ impl<'tcx> Ty<'tcx> {
18861886
}
18871887
}
18881888

1889+
#[inline]
1890+
pub fn is_array_slice(self) -> bool {
1891+
match self.kind() {
1892+
Slice(_) => true,
1893+
RawPtr(TypeAndMut { ty, .. }) | Ref(_, ty, _) => matches!(ty.kind(), Slice(_)),
1894+
_ => false,
1895+
}
1896+
}
1897+
18891898
#[inline]
18901899
pub fn is_array(self) -> bool {
18911900
matches!(self.kind(), Array(..))

compiler/rustc_passes/src/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
202202
self.tcx.sess,
203203
*span,
204204
E0549,
205-
"rustc_deprecated attribute must be paired with \
205+
"deprecated attribute must be paired with \
206206
either stable or unstable attribute"
207207
)
208208
.emit();

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ symbols! {
562562
delay_span_bug_from_inside_query,
563563
deny,
564564
deprecated,
565+
deprecated_suggestion,
565566
deref,
566567
deref_method,
567568
deref_mut,

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::iter;
1111

1212
use super::InferCtxtPrivExt;
1313

14-
crate trait InferCtxtExt<'tcx> {
14+
pub trait InferCtxtExt<'tcx> {
1515
/*private*/
1616
fn impl_similar_to(
1717
&self,
@@ -204,6 +204,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
204204
flags.push((sym::_Self, Some("{integral}".to_owned())));
205205
}
206206

207+
if self_ty.is_array_slice() {
208+
flags.push((sym::_Self, Some("&[]".to_owned())));
209+
}
210+
207211
if let ty::Array(aty, len) = self_ty.kind() {
208212
flags.push((sym::_Self, Some("[]".to_owned())));
209213
flags.push((sym::_Self, Some(format!("[{}]", aty))));

compiler/rustc_typeck/src/check/cast.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ pub enum CastError {
165165
NonScalar,
166166
UnknownExprPtrKind,
167167
UnknownCastPtrKind,
168+
/// Cast of int to (possibly) fat raw pointer.
169+
///
170+
/// Argument is the specific name of the metadata in plain words, such as "a vtable"
171+
/// or "a length". If this argument is None, then the metadata is unknown, for example,
172+
/// when we're typechecking a type parameter with a ?Sized bound.
173+
IntToFatCast(Option<&'static str>),
168174
}
169175

170176
impl From<ErrorGuaranteed> for CastError {
@@ -522,6 +528,35 @@ impl<'a, 'tcx> CastCheck<'tcx> {
522528
.diagnostic()
523529
.emit();
524530
}
531+
CastError::IntToFatCast(known_metadata) => {
532+
let mut err = struct_span_err!(
533+
fcx.tcx.sess,
534+
self.cast_span,
535+
E0606,
536+
"cannot cast `{}` to a pointer that {} wide",
537+
fcx.ty_to_string(self.expr_ty),
538+
if known_metadata.is_some() { "is" } else { "may be" }
539+
);
540+
541+
err.span_label(
542+
self.cast_span,
543+
format!(
544+
"creating a `{}` requires both an address and {}",
545+
self.cast_ty,
546+
known_metadata.unwrap_or("type-specific metadata"),
547+
),
548+
);
549+
550+
if fcx.tcx.sess.is_nightly_build() {
551+
err.span_label(
552+
self.expr.span,
553+
"consider casting this expression to `*const ()`, \
554+
then using `core::ptr::from_raw_parts`",
555+
);
556+
}
557+
558+
err.emit();
559+
}
525560
CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
526561
let unknown_cast_to = match e {
527562
CastError::UnknownCastPtrKind => true,
@@ -900,7 +935,13 @@ impl<'a, 'tcx> CastCheck<'tcx> {
900935
match fcx.pointer_kind(m_cast.ty, self.span)? {
901936
None => Err(CastError::UnknownCastPtrKind),
902937
Some(PointerKind::Thin) => Ok(CastKind::AddrPtrCast),
903-
_ => Err(CastError::IllegalCast),
938+
Some(PointerKind::Vtable(_)) => Err(CastError::IntToFatCast(Some("a vtable"))),
939+
Some(PointerKind::Length) => Err(CastError::IntToFatCast(Some("a length"))),
940+
Some(
941+
PointerKind::OfProjection(_)
942+
| PointerKind::OfOpaque(_, _)
943+
| PointerKind::OfParam(_),
944+
) => Err(CastError::IntToFatCast(None)),
904945
}
905946
}
906947

0 commit comments

Comments
 (0)