|
| 1 | +// #![deny(rustc::untranslatable_diagnostic)] |
| 2 | +// #![deny(rustc::diagnostic_outside_of_impl)] |
| 3 | + |
1 | 4 | use rustc_errors::{
|
2 | 5 | struct_span_err, DiagnosticBuilder, DiagnosticId, DiagnosticMessage, ErrorGuaranteed, MultiSpan,
|
3 | 6 | };
|
4 | 7 | use rustc_middle::ty::{self, Ty, TyCtxt};
|
5 | 8 | use rustc_span::Span;
|
6 | 9 |
|
7 |
| -use crate::session_diagnostics::{ |
8 |
| - AssignBorrowErr, BorrowAcrossDestructor, BorrowAcrossGeneratorYield, InteriorDropMoveErr, |
9 |
| - PathShortLive, UseMutBorrowErr, |
| 10 | +use crate::{ |
| 11 | + diagnostics::PlaceAndReason, |
| 12 | + session_diagnostics::{ |
| 13 | + AssignBorrowErr, AssignErr, BorrowAcrossDestructor, BorrowAcrossGeneratorYield, |
| 14 | + InteriorDropMoveErr, MutBorrowErr, PathShortLive, UseMutBorrowErr, |
| 15 | + }, |
10 | 16 | };
|
11 | 17 |
|
12 | 18 | impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
|
@@ -215,9 +221,77 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
|
215 | 221 | pub(crate) fn cannot_assign(
|
216 | 222 | &self,
|
217 | 223 | span: Span,
|
218 |
| - desc: &str, |
| 224 | + path_and_reason: PlaceAndReason<'_>, |
219 | 225 | ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
220 |
| - struct_span_err!(self, span, E0594, "cannot assign to {}", desc) |
| 226 | + let diag = match path_and_reason { |
| 227 | + PlaceAndReason::DeclaredImmute(place, name) => { |
| 228 | + if let Some(name) = name { |
| 229 | + AssignErr::SymbolDeclaredImmute { span, any_place: place, name } |
| 230 | + } else { |
| 231 | + AssignErr::PlaceDeclaredImmute { span, any_place: place } |
| 232 | + } |
| 233 | + } |
| 234 | + PlaceAndReason::InPatternGuard(place) => { |
| 235 | + AssignErr::PatternGuardImmute { span, path: place } |
| 236 | + } |
| 237 | + PlaceAndReason::StaticItem(place, name) => { |
| 238 | + if let Some(name) = name { |
| 239 | + AssignErr::SymbolStatic { span, path: place, static_name: name } |
| 240 | + } else { |
| 241 | + AssignErr::PlaceStatic { span, path: place } |
| 242 | + } |
| 243 | + } |
| 244 | + PlaceAndReason::UpvarCaptured(place) => AssignErr::UpvarInFn { span, path: place }, |
| 245 | + PlaceAndReason::SelfCaptured(place) => AssignErr::CapturedInFn { span, path: place }, |
| 246 | + PlaceAndReason::BehindPointer(place, pointer_ty, name) => { |
| 247 | + if let Some(place) = place { |
| 248 | + match pointer_ty { |
| 249 | + crate::diagnostics::BorrowedContentSource::DerefRawPointer => { |
| 250 | + AssignErr::PlaceBehindRawPointer { span, place } |
| 251 | + } |
| 252 | + crate::diagnostics::BorrowedContentSource::DerefMutableRef => { |
| 253 | + unreachable!() |
| 254 | + } |
| 255 | + crate::diagnostics::BorrowedContentSource::DerefSharedRef => { |
| 256 | + AssignErr::PlaceBehindSharedRef { span, place } |
| 257 | + } |
| 258 | + crate::diagnostics::BorrowedContentSource::OverloadedDeref(_) => { |
| 259 | + AssignErr::PlaceBehindDeref { |
| 260 | + span, |
| 261 | + place, |
| 262 | + name: name.unwrap_or_default(), |
| 263 | + } |
| 264 | + } |
| 265 | + crate::diagnostics::BorrowedContentSource::OverloadedIndex(_) => { |
| 266 | + AssignErr::PlaceBehindIndex { |
| 267 | + span, |
| 268 | + place, |
| 269 | + name: name.unwrap_or_default(), |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | + } else { |
| 274 | + match pointer_ty { |
| 275 | + crate::diagnostics::BorrowedContentSource::DerefRawPointer => { |
| 276 | + AssignErr::DataBehindRawPointer { span } |
| 277 | + } |
| 278 | + crate::diagnostics::BorrowedContentSource::DerefMutableRef => { |
| 279 | + unreachable!() |
| 280 | + } |
| 281 | + crate::diagnostics::BorrowedContentSource::DerefSharedRef => { |
| 282 | + AssignErr::DataBehindSharedRef { span } |
| 283 | + } |
| 284 | + crate::diagnostics::BorrowedContentSource::OverloadedDeref(_) => { |
| 285 | + AssignErr::DataBehindDeref { span, name: name.unwrap_or_default() } |
| 286 | + } |
| 287 | + crate::diagnostics::BorrowedContentSource::OverloadedIndex(_) => { |
| 288 | + AssignErr::DataBehindIndex { span, name: name.unwrap_or_default() } |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | + } |
| 293 | + }; |
| 294 | + self.infcx.tcx.sess.create_err(diag) |
221 | 295 | }
|
222 | 296 |
|
223 | 297 | pub(crate) fn cannot_move_out_of(
|
@@ -285,10 +359,77 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
|
285 | 359 | pub(crate) fn cannot_borrow_path_as_mutable_because(
|
286 | 360 | &self,
|
287 | 361 | span: Span,
|
288 |
| - path: &str, |
289 |
| - reason: &str, |
| 362 | + path_and_reason: PlaceAndReason<'_>, |
290 | 363 | ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
291 |
| - struct_span_err!(self, span, E0596, "cannot borrow {} as mutable{}", path, reason,) |
| 364 | + let diag = match path_and_reason { |
| 365 | + PlaceAndReason::DeclaredImmute(place, name) => { |
| 366 | + if let Some(name) = name { |
| 367 | + MutBorrowErr::SymbolDeclaredImmute { span, any_place: place, name } |
| 368 | + } else { |
| 369 | + MutBorrowErr::PlaceDeclaredImmute { span, any_place: place } |
| 370 | + } |
| 371 | + } |
| 372 | + PlaceAndReason::InPatternGuard(place) => { |
| 373 | + MutBorrowErr::PatternGuardImmute { span, path: place } |
| 374 | + } |
| 375 | + PlaceAndReason::StaticItem(place, name) => { |
| 376 | + if let Some(name) = name { |
| 377 | + MutBorrowErr::SymbolStatic { span, path: place, static_name: name } |
| 378 | + } else { |
| 379 | + MutBorrowErr::PlaceStatic { span, path: place } |
| 380 | + } |
| 381 | + } |
| 382 | + PlaceAndReason::UpvarCaptured(place) => MutBorrowErr::UpvarInFn { span, path: place }, |
| 383 | + PlaceAndReason::SelfCaptured(place) => MutBorrowErr::CapturedInFn { span, path: place }, |
| 384 | + PlaceAndReason::BehindPointer(place, pointer_ty, name) => { |
| 385 | + if let Some(place) = place { |
| 386 | + match pointer_ty { |
| 387 | + crate::diagnostics::BorrowedContentSource::DerefRawPointer => { |
| 388 | + MutBorrowErr::SelfBehindRawPointer { span, place } |
| 389 | + } |
| 390 | + crate::diagnostics::BorrowedContentSource::DerefMutableRef => { |
| 391 | + unreachable!() |
| 392 | + } |
| 393 | + crate::diagnostics::BorrowedContentSource::DerefSharedRef => { |
| 394 | + MutBorrowErr::SelfBehindSharedRef { span, place } |
| 395 | + } |
| 396 | + crate::diagnostics::BorrowedContentSource::OverloadedDeref(_) => { |
| 397 | + MutBorrowErr::SelfBehindDeref { |
| 398 | + span, |
| 399 | + place, |
| 400 | + name: name.unwrap_or_default(), |
| 401 | + } |
| 402 | + } |
| 403 | + crate::diagnostics::BorrowedContentSource::OverloadedIndex(_) => { |
| 404 | + MutBorrowErr::SelfBehindIndex { |
| 405 | + span, |
| 406 | + place, |
| 407 | + name: name.unwrap_or_default(), |
| 408 | + } |
| 409 | + } |
| 410 | + } |
| 411 | + } else { |
| 412 | + match pointer_ty { |
| 413 | + crate::diagnostics::BorrowedContentSource::DerefRawPointer => { |
| 414 | + MutBorrowErr::DataBehindRawPointer { span } |
| 415 | + } |
| 416 | + crate::diagnostics::BorrowedContentSource::DerefMutableRef => { |
| 417 | + unreachable!() |
| 418 | + } |
| 419 | + crate::diagnostics::BorrowedContentSource::DerefSharedRef => { |
| 420 | + MutBorrowErr::DataBehindSharedRef { span } |
| 421 | + } |
| 422 | + crate::diagnostics::BorrowedContentSource::OverloadedDeref(_) => { |
| 423 | + MutBorrowErr::DataBehindDeref { span, name: name.unwrap_or_default() } |
| 424 | + } |
| 425 | + crate::diagnostics::BorrowedContentSource::OverloadedIndex(_) => { |
| 426 | + MutBorrowErr::DataBehindIndex { span, name: name.unwrap_or_default() } |
| 427 | + } |
| 428 | + } |
| 429 | + } |
| 430 | + } |
| 431 | + }; |
| 432 | + self.infcx.tcx.sess.create_err(diag) |
292 | 433 | }
|
293 | 434 |
|
294 | 435 | pub(crate) fn cannot_mutate_in_immutable_section(
|
|
0 commit comments