Skip to content

Commit 9fb32dc

Browse files
committed
Auto merge of rust-lang#99151 - Dylan-DPC:rollup-40aqkxy, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - rust-lang#98882 (explain doc comments in macros a bit) - rust-lang#98907 (Deny float const params even when `adt_const_params` is enabled) - rust-lang#99091 (Do not mention private types from other crates as impl candidates) - rust-lang#99140 (Implement `SourceMap::is_span_accessible`) - rust-lang#99147 (Mention similarly named associated type even if it's not clearly in supertrait) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 7d1f57a + 21d6b1f commit 9fb32dc

File tree

31 files changed

+348
-96
lines changed

31 files changed

+348
-96
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
309309
));
310310

311311
// Check first whether the source is accessible (issue #87060)
312-
if self.infcx.tcx.sess.source_map().span_to_snippet(deref_target).is_ok() {
312+
if self.infcx.tcx.sess.source_map().is_span_accessible(deref_target) {
313313
err.span_note(deref_target, "deref defined here");
314314
}
315315
}

Diff for: compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -975,14 +975,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
975975
if self.fn_self_span_reported.insert(fn_span) {
976976
err.span_note(
977977
// Check whether the source is accessible
978-
if self
979-
.infcx
980-
.tcx
981-
.sess
982-
.source_map()
983-
.span_to_snippet(self_arg.span)
984-
.is_ok()
985-
{
978+
if self.infcx.tcx.sess.source_map().is_span_accessible(self_arg.span) {
986979
self_arg.span
987980
} else {
988981
fn_call_span

Diff for: compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
299299
err.note(&format!("attempting to deref into `{}`", deref_target_ty));
300300

301301
// Check first whether the source is accessible (issue #87060)
302-
if tcx.sess.source_map().span_to_snippet(deref_target).is_ok() {
302+
if tcx.sess.source_map().is_span_accessible(deref_target) {
303303
err.span_note(deref_target, "deref defined here");
304304
}
305305

Diff for: compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl Qualif for CustomEq {
226226
// because that component may be part of an enum variant (e.g.,
227227
// `Option::<NonStructuralMatchTy>::Some`), in which case some values of this type may be
228228
// structural-match (`Option::None`).
229-
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty).is_some()
229+
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty, true).is_some()
230230
}
231231

232232
fn in_adt_inherently<'tcx>(
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
expand-explain-doc-comment-outer =
2+
outer doc comments expand to `#[doc = "..."]`, which is what this macro attempted to match
3+
4+
expand-explain-doc-comment-inner =
5+
inner doc comments expand to `#![doc = "..."]`, which is what this macro attempted to match

Diff for: compiler/rustc_error_messages/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ pub use unic_langid::{langid, LanguageIdentifier};
3333
fluent_messages! {
3434
borrowck => "../locales/en-US/borrowck.ftl",
3535
builtin_macros => "../locales/en-US/builtin_macros.ftl",
36+
const_eval => "../locales/en-US/const_eval.ftl",
37+
expand => "../locales/en-US/expand.ftl",
3638
lint => "../locales/en-US/lint.ftl",
3739
parser => "../locales/en-US/parser.ftl",
3840
privacy => "../locales/en-US/privacy.ftl",
3941
typeck => "../locales/en-US/typeck.ftl",
40-
const_eval => "../locales/en-US/const_eval.ftl",
4142
}
4243

4344
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};

Diff for: compiler/rustc_errors/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ pub fn add_elided_lifetime_in_path_suggestion(
15581558
insertion_span: Span,
15591559
) {
15601560
diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n)));
1561-
if source_map.span_to_snippet(insertion_span).is_err() {
1561+
if !source_map.is_span_accessible(insertion_span) {
15621562
// Do not try to suggest anything if generated by a proc-macro.
15631563
return;
15641564
}

Diff for: compiler/rustc_expand/src/mbe/macro_rules.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::{NodeId, DUMMY_NODE_ID};
1414
use rustc_ast_pretty::pprust;
1515
use rustc_attr::{self as attr, TransparencyError};
1616
use rustc_data_structures::fx::FxHashMap;
17-
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder};
17+
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
1818
use rustc_feature::Features;
1919
use rustc_lint_defs::builtin::{
2020
RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
@@ -25,6 +25,7 @@ use rustc_session::parse::ParseSess;
2525
use rustc_session::Session;
2626
use rustc_span::edition::Edition;
2727
use rustc_span::hygiene::Transparency;
28+
use rustc_span::source_map::SourceMap;
2829
use rustc_span::symbol::{kw, sym, Ident, MacroRulesNormalizedIdent};
2930
use rustc_span::Span;
3031

@@ -345,7 +346,7 @@ fn expand_macro<'cx>(
345346
if !def_span.is_dummy() && !cx.source_map().is_imported(def_span) {
346347
err.span_label(cx.source_map().guess_head_span(def_span), "when calling this macro");
347348
}
348-
349+
annotate_doc_comment(&mut err, sess.source_map(), span);
349350
// Check whether there's a missing comma in this macro call, like `println!("{}" a);`
350351
if let Some((arg, comma_span)) = arg.add_comma() {
351352
for lhs in lhses {
@@ -453,7 +454,10 @@ pub fn compile_declarative_macro(
453454
Failure(token, msg) => {
454455
let s = parse_failure_msg(&token);
455456
let sp = token.span.substitute_dummy(def.span);
456-
sess.parse_sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
457+
let mut err = sess.parse_sess.span_diagnostic.struct_span_err(sp, &s);
458+
err.span_label(sp, msg);
459+
annotate_doc_comment(&mut err, sess.source_map(), sp);
460+
err.emit();
457461
return dummy_syn_ext();
458462
}
459463
Error(sp, msg) => {
@@ -590,6 +594,34 @@ pub fn compile_declarative_macro(
590594
(mk_syn_ext(expander), rule_spans)
591595
}
592596

597+
#[derive(SessionSubdiagnostic)]
598+
enum ExplainDocComment {
599+
#[label(expand::explain_doc_comment_inner)]
600+
Inner {
601+
#[primary_span]
602+
span: Span,
603+
},
604+
#[label(expand::explain_doc_comment_outer)]
605+
Outer {
606+
#[primary_span]
607+
span: Span,
608+
},
609+
}
610+
611+
fn annotate_doc_comment(
612+
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
613+
sm: &SourceMap,
614+
span: Span,
615+
) {
616+
if let Ok(src) = sm.span_to_snippet(span) {
617+
if src.starts_with("///") || src.starts_with("/**") {
618+
err.subdiagnostic(ExplainDocComment::Outer { span });
619+
} else if src.starts_with("//!") || src.starts_with("/*!") {
620+
err.subdiagnostic(ExplainDocComment::Inner { span });
621+
}
622+
}
623+
}
624+
593625
fn check_lhs_nt_follows(sess: &ParseSess, def: &ast::Item, lhs: &mbe::TokenTree) -> bool {
594626
// lhs is going to be like TokenTree::Delimited(...), where the
595627
// entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
432432
"`let` bindings require an \"irrefutable pattern\", like a `struct` or \
433433
an `enum` with only one variant",
434434
);
435-
if self.tcx.sess.source_map().span_to_snippet(span).is_ok() {
435+
if self.tcx.sess.source_map().is_span_accessible(span) {
436436
let semi_span = span.shrink_to_hi().with_lo(span.hi() - BytePos(1));
437437
let start_span = span.shrink_to_lo();
438438
let end_span = semi_span.shrink_to_lo();

Diff for: compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+31-26
Original file line numberDiff line numberDiff line change
@@ -120,32 +120,37 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
120120
}
121121

122122
fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
123-
traits::search_for_structural_match_violation(self.span, self.tcx(), ty).map(|non_sm_ty| {
124-
with_no_trimmed_paths!(match non_sm_ty.kind {
125-
traits::NonStructuralMatchTyKind::Adt(adt) => self.adt_derive_msg(adt),
126-
traits::NonStructuralMatchTyKind::Dynamic => {
127-
"trait objects cannot be used in patterns".to_string()
128-
}
129-
traits::NonStructuralMatchTyKind::Opaque => {
130-
"opaque types cannot be used in patterns".to_string()
131-
}
132-
traits::NonStructuralMatchTyKind::Closure => {
133-
"closures cannot be used in patterns".to_string()
134-
}
135-
traits::NonStructuralMatchTyKind::Generator => {
136-
"generators cannot be used in patterns".to_string()
137-
}
138-
traits::NonStructuralMatchTyKind::Param => {
139-
bug!("use of a constant whose type is a parameter inside a pattern")
140-
}
141-
traits::NonStructuralMatchTyKind::Projection => {
142-
bug!("use of a constant whose type is a projection inside a pattern")
143-
}
144-
traits::NonStructuralMatchTyKind::Foreign => {
145-
bug!("use of a value of a foreign type inside a pattern")
146-
}
147-
})
148-
})
123+
traits::search_for_structural_match_violation(self.span, self.tcx(), ty, true).map(
124+
|non_sm_ty| {
125+
with_no_trimmed_paths!(match non_sm_ty.kind {
126+
traits::NonStructuralMatchTyKind::Adt(adt) => self.adt_derive_msg(adt),
127+
traits::NonStructuralMatchTyKind::Dynamic => {
128+
"trait objects cannot be used in patterns".to_string()
129+
}
130+
traits::NonStructuralMatchTyKind::Opaque => {
131+
"opaque types cannot be used in patterns".to_string()
132+
}
133+
traits::NonStructuralMatchTyKind::Closure => {
134+
"closures cannot be used in patterns".to_string()
135+
}
136+
traits::NonStructuralMatchTyKind::Generator => {
137+
"generators cannot be used in patterns".to_string()
138+
}
139+
traits::NonStructuralMatchTyKind::Float => {
140+
"floating-point numbers cannot be used in patterns".to_string()
141+
}
142+
traits::NonStructuralMatchTyKind::Param => {
143+
bug!("use of a constant whose type is a parameter inside a pattern")
144+
}
145+
traits::NonStructuralMatchTyKind::Projection => {
146+
bug!("use of a constant whose type is a projection inside a pattern")
147+
}
148+
traits::NonStructuralMatchTyKind::Foreign => {
149+
bug!("use of a value of a foreign type inside a pattern")
150+
}
151+
})
152+
},
153+
)
149154
}
150155

151156
fn type_marked_structural(&self, ty: Ty<'tcx>) -> bool {

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ impl<'a> Resolver<'a> {
16471647

16481648
fn binding_description(&self, b: &NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {
16491649
let res = b.res();
1650-
if b.span.is_dummy() || self.session.source_map().span_to_snippet(b.span).is_err() {
1650+
if b.span.is_dummy() || !self.session.source_map().is_span_accessible(b.span) {
16511651
// These already contain the "built-in" prefix or look bad with it.
16521652
let add_built_in =
16531653
!matches!(b.res(), Res::NonMacroAttr(..) | Res::PrimTy(..) | Res::ToolMod);

Diff for: compiler/rustc_span/src/source_map.rs

+7
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,13 @@ impl SourceMap {
597597
local_begin.sf.src.is_some() && local_end.sf.src.is_some()
598598
}
599599

600+
pub fn is_span_accessible(&self, sp: Span) -> bool {
601+
self.span_to_source(sp, |src, start_index, end_index| {
602+
Ok(src.get(start_index..end_index).is_some())
603+
})
604+
.map_or(false, |is_accessible| is_accessible)
605+
}
606+
600607
/// Returns the source snippet as `String` corresponding to the given `Span`.
601608
pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
602609
self.span_to_source(sp, |src, start_index, end_index| {

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
673673
if !self.report_similar_impl_candidates(
674674
impl_candidates,
675675
trait_ref,
676+
obligation.cause.body_id,
676677
&mut err,
677678
) {
678679
// This is *almost* equivalent to
@@ -707,6 +708,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
707708
self.report_similar_impl_candidates(
708709
impl_candidates,
709710
trait_ref,
711+
obligation.cause.body_id,
710712
&mut err,
711713
);
712714
}
@@ -1353,6 +1355,7 @@ trait InferCtxtPrivExt<'hir, 'tcx> {
13531355
&self,
13541356
impl_candidates: Vec<ImplCandidate<'tcx>>,
13551357
trait_ref: ty::PolyTraitRef<'tcx>,
1358+
body_id: hir::HirId,
13561359
err: &mut Diagnostic,
13571360
) -> bool;
13581361

@@ -1735,6 +1738,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
17351738
&self,
17361739
impl_candidates: Vec<ImplCandidate<'tcx>>,
17371740
trait_ref: ty::PolyTraitRef<'tcx>,
1741+
body_id: hir::HirId,
17381742
err: &mut Diagnostic,
17391743
) -> bool {
17401744
let report = |mut candidates: Vec<TraitRef<'tcx>>, err: &mut Diagnostic| {
@@ -1805,8 +1809,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
18051809
|| self.tcx.is_builtin_derive(def_id)
18061810
})
18071811
.filter_map(|def_id| self.tcx.impl_trait_ref(def_id))
1808-
// Avoid mentioning type parameters.
1809-
.filter(|trait_ref| !matches!(trait_ref.self_ty().kind(), ty::Param(_)))
1812+
.filter(|trait_ref| {
1813+
let self_ty = trait_ref.self_ty();
1814+
// Avoid mentioning type parameters.
1815+
if let ty::Param(_) = self_ty.kind() {
1816+
false
1817+
}
1818+
// Avoid mentioning types that are private to another crate
1819+
else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
1820+
// FIXME(compiler-errors): This could be generalized, both to
1821+
// be more granular, and probably look past other `#[fundamental]`
1822+
// types, too.
1823+
self.tcx
1824+
.visibility(def.did())
1825+
.is_accessible_from(body_id.owner.to_def_id(), self.tcx)
1826+
} else {
1827+
true
1828+
}
1829+
})
18101830
.collect();
18111831
return report(normalized_impl_candidates, err);
18121832
}

Diff for: compiler/rustc_trait_selection/src/traits/structural_match.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub enum NonStructuralMatchTyKind<'tcx> {
2626
Closure,
2727
Generator,
2828
Projection,
29+
Float,
2930
}
3031

3132
/// This method traverses the structure of `ty`, trying to find an
@@ -53,12 +54,16 @@ pub enum NonStructuralMatchTyKind<'tcx> {
5354
/// For more background on why Rust has this requirement, and issues
5455
/// that arose when the requirement was not enforced completely, see
5556
/// Rust RFC 1445, rust-lang/rust#61188, and rust-lang/rust#62307.
57+
///
58+
/// The floats_allowed flag is used to deny constants in floating point
5659
pub fn search_for_structural_match_violation<'tcx>(
5760
span: Span,
5861
tcx: TyCtxt<'tcx>,
5962
ty: Ty<'tcx>,
63+
floats_allowed: bool,
6064
) -> Option<NonStructuralMatchTy<'tcx>> {
61-
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default() }).break_value()
65+
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), floats_allowed })
66+
.break_value()
6267
}
6368

6469
/// This method returns true if and only if `adt_ty` itself has been marked as
@@ -119,6 +124,8 @@ struct Search<'tcx> {
119124
/// Tracks ADTs previously encountered during search, so that
120125
/// we will not recur on them again.
121126
seen: FxHashSet<hir::def_id::DefId>,
127+
128+
floats_allowed: bool,
122129
}
123130

124131
impl<'tcx> Search<'tcx> {
@@ -192,13 +199,24 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
192199
// for empty array.
193200
return ControlFlow::CONTINUE;
194201
}
195-
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str | ty::Never => {
202+
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Str | ty::Never => {
196203
// These primitive types are always structural match.
197204
//
198205
// `Never` is kind of special here, but as it is not inhabitable, this should be fine.
199206
return ControlFlow::CONTINUE;
200207
}
201208

209+
ty::Float(_) => {
210+
if self.floats_allowed {
211+
return ControlFlow::CONTINUE;
212+
} else {
213+
return ControlFlow::Break(NonStructuralMatchTy {
214+
ty,
215+
kind: NonStructuralMatchTyKind::Float,
216+
});
217+
}
218+
}
219+
202220
ty::Array(..) | ty::Slice(_) | ty::Ref(..) | ty::Tuple(..) => {
203221
// First check all contained types and then tell the caller to continue searching.
204222
return ty.super_visit_with(self);

0 commit comments

Comments
 (0)