From 4547b622d8d29df964fa2914213088b148c498fc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:32 +0200 Subject: Merging upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_span/src/analyze_source_file.rs | 4 +- compiler/rustc_span/src/caching_source_map_view.rs | 2 +- compiler/rustc_span/src/def_id.rs | 12 +-- compiler/rustc_span/src/hygiene.rs | 4 +- compiler/rustc_span/src/lib.rs | 20 +++-- compiler/rustc_span/src/source_map.rs | 91 ++++++++++++++++++---- compiler/rustc_span/src/source_map/tests.rs | 11 +-- compiler/rustc_span/src/span_encoding.rs | 2 +- compiler/rustc_span/src/symbol.rs | 70 ++++------------- 9 files changed, 118 insertions(+), 98 deletions(-) (limited to 'compiler/rustc_span/src') diff --git a/compiler/rustc_span/src/analyze_source_file.rs b/compiler/rustc_span/src/analyze_source_file.rs index 5987fb2a1..d3c2c5113 100644 --- a/compiler/rustc_span/src/analyze_source_file.rs +++ b/compiler/rustc_span/src/analyze_source_file.rs @@ -41,7 +41,7 @@ pub fn analyze_source_file( } cfg_if::cfg_if! { - if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64")))] { + if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { fn analyze_source_file_dispatch(src: &str, source_file_start_pos: BytePos, lines: &mut Vec, @@ -247,7 +247,7 @@ fn analyze_source_file_generic( // The slow path: // This is either ASCII control character "DEL" or the beginning of // a multibyte char. Just decode to `char`. - let c = (&src[i..]).chars().next().unwrap(); + let c = src[i..].chars().next().unwrap(); char_len = c.len_utf8(); let pos = BytePos::from_usize(i) + output_offset; diff --git a/compiler/rustc_span/src/caching_source_map_view.rs b/compiler/rustc_span/src/caching_source_map_view.rs index fdabf404a..886112769 100644 --- a/compiler/rustc_span/src/caching_source_map_view.rs +++ b/compiler/rustc_span/src/caching_source_map_view.rs @@ -165,7 +165,7 @@ impl<'sm> CachingSourceMapView<'sm> { Some(new_file_and_idx) } else { let file = &self.line_cache[oldest].file; - if !file_contains(&file, span_data.hi) { + if !file_contains(file, span_data.hi) { return None; } diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index bbeabdb55..e62ce2c26 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -1,4 +1,4 @@ -use crate::HashStableContext; +use crate::{HashStableContext, Symbol}; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use rustc_data_structures::AtomicRef; @@ -34,7 +34,7 @@ impl CrateNum { impl fmt::Display for CrateNum { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&self.private, f) + fmt::Display::fmt(&self.as_u32(), f) } } @@ -149,9 +149,11 @@ impl StableCrateId { /// Computes the stable ID for a crate with the given name and /// `-Cmetadata` arguments. - pub fn new(crate_name: &str, is_exe: bool, mut metadata: Vec) -> StableCrateId { + pub fn new(crate_name: Symbol, is_exe: bool, mut metadata: Vec) -> StableCrateId { let mut hasher = StableHasher::new(); - crate_name.hash(&mut hasher); + // We must hash the string text of the crate name, not the id, as the id is not stable + // across builds. + crate_name.as_str().hash(&mut hasher); // We don't want the stable crate ID to depend on the order of // -C metadata arguments, so sort them: @@ -274,7 +276,7 @@ impl Ord for DefId { impl PartialOrd for DefId { #[inline] fn partial_cmp(&self, other: &DefId) -> Option { - Some(Ord::cmp(self, other)) + Some(self.cmp(other)) } } diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 191186af6..038699154 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -76,7 +76,7 @@ pub struct ExpnId { impl fmt::Debug for ExpnId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Generate crate_::{{expn_}}. - write!(f, "{:?}::{{{{expn{}}}}}", self.krate, self.local_id.private) + write!(f, "{:?}::{{{{expn{}}}}}", self.krate, self.local_id.as_u32()) } } @@ -381,7 +381,7 @@ impl HygieneData { } pub fn with T>(f: F) -> T { - with_session_globals(|session_globals| f(&mut *session_globals.hygiene_data.borrow_mut())) + with_session_globals(|session_globals| f(&mut session_globals.hygiene_data.borrow_mut())) } #[inline] diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 322c7104b..cef4c6f79 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -78,10 +78,10 @@ use sha2::Sha256; #[cfg(test)] mod tests; -// Per-session global variables: this struct is stored in thread-local storage -// in such a way that it is accessible without any kind of handle to all -// threads within the compilation session, but is not accessible outside the -// session. +/// Per-session global variables: this struct is stored in thread-local storage +/// in such a way that it is accessible without any kind of handle to all +/// threads within the compilation session, but is not accessible outside the +/// session. pub struct SessionGlobals { symbol_interner: symbol::Interner, span_interner: Lock, @@ -217,9 +217,7 @@ impl RealFileName { pub fn local_path(&self) -> Option<&Path> { match self { RealFileName::LocalPath(p) => Some(p), - RealFileName::Remapped { local_path: p, virtual_name: _ } => { - p.as_ref().map(PathBuf::as_path) - } + RealFileName::Remapped { local_path, virtual_name: _ } => local_path.as_deref(), } } @@ -240,7 +238,7 @@ impl RealFileName { pub fn remapped_path_if_available(&self) -> &Path { match self { RealFileName::LocalPath(p) - | RealFileName::Remapped { local_path: _, virtual_name: p } => &p, + | RealFileName::Remapped { local_path: _, virtual_name: p } => p, } } @@ -361,8 +359,8 @@ impl FileName { FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Remapped } } - // This may include transient local filesystem information. - // Must not be embedded in build outputs. + /// This may include transient local filesystem information. + /// Must not be embedded in build outputs. pub fn prefer_local(&self) -> FileNameDisplay<'_> { FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Local } } @@ -753,7 +751,7 @@ impl Span { /// Checks if a span is "internal" to a macro in which `unsafe` /// can be used without triggering the `unsafe_code` lint. - // (that is, a macro marked with `#[allow_internal_unsafe]`). + /// (that is, a macro marked with `#[allow_internal_unsafe]`). pub fn allows_unsafe(self) -> bool { self.ctxt().outer_expn_data().allow_internal_unsafe } diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index f9566eeee..2ae57d9e5 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -130,14 +130,14 @@ impl FileLoader for RealFileLoader { /// different has no real downsides. #[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)] pub struct StableSourceFileId { - // A hash of the source file's FileName. This is hash so that it's size - // is more predictable than if we included the actual FileName value. + /// A hash of the source file's [`FileName`]. This is hash so that it's size + /// is more predictable than if we included the actual [`FileName`] value. pub file_name_hash: u64, - // The CrateNum of the crate this source file was originally parsed for. - // We cannot include this information in the hash because at the time - // of hashing we don't have the context to map from the CrateNum's numeric - // value to a StableCrateId. + /// The [`CrateNum`] of the crate this source file was originally parsed for. + /// We cannot include this information in the hash because at the time + /// of hashing we don't have the context to map from the [`CrateNum`]'s numeric + /// value to a `StableCrateId`. pub cnum: CrateNum, } @@ -402,7 +402,7 @@ impl SourceMap { source_file } - // If there is a doctest offset, applies it to the line. + /// If there is a doctest offset, applies it to the line. pub fn doctest_offset_line(&self, file: &FileName, orig: usize) -> usize { match file { FileName::DocTest(_, offset) => { @@ -429,7 +429,7 @@ impl SourceMap { Loc { file: sf, line, col, col_display } } - // If the corresponding `SourceFile` is empty, does not return a line number. + /// If the corresponding `SourceFile` is empty, does not return a line number. pub fn lookup_line(&self, pos: BytePos) -> Result> { let f = self.lookup_source_file(pos); @@ -753,6 +753,67 @@ impl SourceMap { } } + /// Given a 'Span', tries to tell if it's wrapped by "<>" or "()" + /// the algorithm searches if the next character is '>' or ')' after skipping white space + /// then searches the previous charactoer to match '<' or '(' after skipping white space + /// return true if wrapped by '<>' or '()' + pub fn span_wrapped_by_angle_or_parentheses(&self, span: Span) -> bool { + self.span_to_source(span, |src, start_index, end_index| { + if src.get(start_index..end_index).is_none() { + return Ok(false); + } + // test the right side to match '>' after skipping white space + let end_src = &src[end_index..]; + let mut i = 0; + let mut found_right_parentheses = false; + let mut found_right_angle = false; + while let Some(cc) = end_src.chars().nth(i) { + if cc == ' ' { + i = i + 1; + } else if cc == '>' { + // found > in the right; + found_right_angle = true; + break; + } else if cc == ')' { + found_right_parentheses = true; + break; + } else { + // failed to find '>' return false immediately + return Ok(false); + } + } + // test the left side to match '<' after skipping white space + i = start_index; + let start_src = &src[0..start_index]; + while let Some(cc) = start_src.chars().nth(i) { + if cc == ' ' { + if i == 0 { + return Ok(false); + } + i = i - 1; + } else if cc == '<' { + // found < in the left + if !found_right_angle { + // skip something like "(< )>" + return Ok(false); + } + break; + } else if cc == '(' { + if !found_right_parentheses { + // skip something like "<(>)" + return Ok(false); + } + break; + } else { + // failed to find '<' return false immediately + return Ok(false); + } + } + return Ok(true); + }) + .map_or(false, |is_accessible| is_accessible) + } + /// Given a `Span`, tries to get a shorter span ending just after the first occurrence of `char` /// `c`. pub fn span_through_char(&self, sp: Span, c: char) -> Span { @@ -855,7 +916,8 @@ impl SourceMap { /// Returns a new span representing the next character after the end-point of this span. /// Special cases: /// - if span is a dummy one, returns the same span - /// - if next_point reached the end of source, return span with lo = hi + /// - if next_point reached the end of source, return a span exceeding the end of source, + /// which means sm.span_to_snippet(next_point) will get `Err` /// - respect multi-byte characters pub fn next_point(&self, sp: Span) -> Span { if sp.is_dummy() { @@ -864,9 +926,6 @@ impl SourceMap { let start_of_next_point = sp.hi().0; let width = self.find_width_of_character_at_span(sp, true); - if width == 0 { - return Span::new(sp.hi(), sp.hi(), sp.ctxt(), None); - } // If the width is 1, then the next span should only contain the next char besides current ending. // However, in the case of a multibyte character, where the width != 1, the next span should // span multiple bytes to include the whole character. @@ -938,7 +997,7 @@ impl SourceMap { // Ensure indexes are also not malformed. if start_index > end_index || end_index > source_len - 1 { debug!("find_width_of_character_at_span: source indexes are malformed"); - return 0; + return 1; } let src = local_begin.sf.external_src.borrow(); @@ -994,9 +1053,9 @@ impl SourceMap { SourceFileAndBytePos { sf, pos: offset } } - // Returns the index of the `SourceFile` (in `self.files`) that contains `pos`. - // This index is guaranteed to be valid for the lifetime of this `SourceMap`, - // since `source_files` is a `MonotonicVec` + /// Returns the index of the [`SourceFile`] (in `self.files`) that contains `pos`. + /// This index is guaranteed to be valid for the lifetime of this `SourceMap`, + /// since `source_files` is a `MonotonicVec` pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize { self.files .borrow() diff --git a/compiler/rustc_span/src/source_map/tests.rs b/compiler/rustc_span/src/source_map/tests.rs index 1fd81018f..3cab59e8d 100644 --- a/compiler/rustc_span/src/source_map/tests.rs +++ b/compiler/rustc_span/src/source_map/tests.rs @@ -511,16 +511,17 @@ fn test_next_point() { assert_eq!(span.lo().0, 4); assert_eq!(span.hi().0, 5); - // A non-empty span at the last byte should advance to create an empty - // span pointing at the end of the file. + // Reaching to the end of file, return a span that will get error with `span_to_snippet` let span = Span::with_root_ctxt(BytePos(4), BytePos(5)); let span = sm.next_point(span); assert_eq!(span.lo().0, 5); - assert_eq!(span.hi().0, 5); + assert_eq!(span.hi().0, 6); + assert!(sm.span_to_snippet(span).is_err()); - // Empty span pointing just past the last byte. + // Reaching to the end of file, return a span that will get error with `span_to_snippet` let span = Span::with_root_ctxt(BytePos(5), BytePos(5)); let span = sm.next_point(span); assert_eq!(span.lo().0, 5); - assert_eq!(span.hi().0, 5); + assert_eq!(span.hi().0, 6); + assert!(sm.span_to_snippet(span).is_err()); } diff --git a/compiler/rustc_span/src/span_encoding.rs b/compiler/rustc_span/src/span_encoding.rs index b3de67415..f0e91e5a6 100644 --- a/compiler/rustc_span/src/span_encoding.rs +++ b/compiler/rustc_span/src/span_encoding.rs @@ -166,5 +166,5 @@ impl SpanInterner { // If an interner exists, return it. Otherwise, prepare a fresh one. #[inline] fn with_span_interner T>(f: F) -> T { - crate::with_session_globals(|session_globals| f(&mut *session_globals.span_interner.lock())) + crate::with_session_globals(|session_globals| f(&mut session_globals.span_interner.lock())) } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 7f16da52b..9e446c96d 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -170,8 +170,6 @@ symbols! { Count, Cow, Debug, - DebugStruct, - DebugTuple, Decodable, Decoder, DecorateLint, @@ -190,9 +188,6 @@ symbols! { Error, File, FileType, - Fn, - FnMut, - FnOnce, FormatSpec, Formatter, From, @@ -211,7 +206,6 @@ symbols! { Input, Into, IntoDiagnostic, - IntoFuture, IntoIterator, IoRead, IoWrite, @@ -256,7 +250,6 @@ symbols! { Pointer, Poll, ProcMacro, - ProcMacroHack, ProceduralMasqueradeDummyType, Range, RangeFrom, @@ -271,6 +264,7 @@ symbols! { Relaxed, Release, Result, + ResumeTy, Return, Right, Rust, @@ -332,7 +326,6 @@ symbols! { abi_vectorcall, abi_x86_interrupt, abort, - aborts, add, add_assign, add_with_overflow, @@ -344,7 +337,6 @@ symbols! { align, align_offset, alignment, - alignstack, all, alloc, alloc_error_handler, @@ -433,7 +425,6 @@ symbols! { bool, borrowck_graphviz_format, borrowck_graphviz_postflow, - borrowck_graphviz_preflow, box_free, box_patterns, box_syntax, @@ -462,7 +453,6 @@ symbols! { cfg_doctest, cfg_eval, cfg_hide, - cfg_macro, cfg_panic, cfg_sanitize, cfg_target_abi, @@ -470,7 +460,6 @@ symbols! { cfg_target_feature, cfg_target_has_atomic, cfg_target_has_atomic_equal_alignment, - cfg_target_has_atomic_load_store, cfg_target_thread_local, cfg_target_vendor, cfg_version, @@ -495,19 +484,15 @@ symbols! { cold, collapse_debuginfo, column, - column_macro, - compare_and_swap, compare_exchange, compare_exchange_weak, compile_error, - compile_error_macro, compiler, compiler_builtins, compiler_fence, concat, concat_bytes, concat_idents, - concat_macro, conservative_impl_trait, console, const_allocate, @@ -528,7 +513,6 @@ symbols! { const_fn_unsize, const_for, const_format_args, - const_generic_defaults, const_generics, const_generics_defaults, const_if_match, @@ -547,22 +531,19 @@ symbols! { const_trait, const_trait_bound_opt_out, const_trait_impl, - const_transmute, const_try, constant, constructor, - contents, context, - convert, copy, copy_closures, copy_nonoverlapping, copysignf32, copysignf64, core, - core_intrinsics, core_panic, core_panic_2015_macro, + core_panic_2021_macro, core_panic_macro, cosf32, cosf64, @@ -584,6 +565,7 @@ symbols! { custom_attribute, custom_derive, custom_inner_attributes, + custom_mir, custom_test_frameworks, d, d32, @@ -597,7 +579,6 @@ symbols! { debug_assertions, debug_struct, debug_struct_fields_finish, - debug_trait_builder, debug_tuple, debug_tuple_fields_finish, debugger_visualizer, @@ -619,6 +600,7 @@ symbols! { deref_mut, deref_target, derive, + derive_const, derive_default_enum, destruct, destructuring_assignment, @@ -628,7 +610,6 @@ symbols! { discriminant_type, discriminant_value, dispatch_from_dyn, - display_trait, div, div_assign, doc, @@ -659,7 +640,6 @@ symbols! { dyn_star, dyn_trait, e, - edition_macro_pats, edition_panic, eh_catch_typeinfo, eh_personality, @@ -672,7 +652,6 @@ symbols! { encode, end, env, - env_macro, eprint_macro, eprintln_macro, eq, @@ -694,6 +673,7 @@ symbols! { export_name, expr, extended_key_value_attributes, + extended_varargs_abi_support, extern_absolute_paths, extern_crate_item_prelude, extern_crate_self, @@ -721,9 +701,7 @@ symbols! { field, field_init_shorthand, file, - file_macro, fill, - finish, flags, float, float_to_int_unchecked, @@ -732,8 +710,6 @@ symbols! { fmaf32, fmaf64, fmt, - fmt_as_str, - fmt_internals, fmul_fast, fn_align, fn_must_use, @@ -748,13 +724,11 @@ symbols! { format_args_macro, format_args_nl, format_macro, - fp, freeze, freg, frem_fast, from, from_desugaring, - from_generator, from_iter, from_method, from_output, @@ -805,14 +779,16 @@ symbols! { i64, i8, ident, + identity_future, if_let, if_let_guard, if_while_or_patterns, ignore, impl_header_lifetime_elision, impl_lint_pass, - impl_macros, impl_trait_in_bindings, + impl_trait_in_fn_trait_return, + impl_trait_projections, implied_by, import, import_name_type, @@ -822,7 +798,6 @@ symbols! { include, include_bytes, include_bytes_macro, - include_macro, include_str, include_str_macro, inclusive_range_syntax, @@ -840,7 +815,6 @@ symbols! { instruction_set, integer_: "integer", integral, - intel, into_future, into_iter, intra_doc_pointers, @@ -877,7 +851,6 @@ symbols! { lifetimes, likely, line, - line_macro, link, link_args, link_cfg, @@ -961,7 +934,6 @@ symbols! { modifiers, module, module_path, - module_path_macro, more_qualified_paths, more_struct_aliases, movbe_target_feature, @@ -1031,7 +1003,6 @@ symbols! { non_exhaustive, non_exhaustive_omitted_patterns_lint, non_modrs_mods, - none_error, nontemporal_store, noop_method_borrow, noop_method_clone, @@ -1056,7 +1027,6 @@ symbols! { optin_builtin_traits, option, option_env, - option_env_macro, options, or, or_patterns, @@ -1099,7 +1069,7 @@ symbols! { plugins, pointee_trait, pointer, - pointer_trait_fmt, + pointer_sized, poll, position, post_dash_lto: "post-lto", @@ -1126,7 +1096,6 @@ symbols! { proc_dash_macro: "proc-macro", proc_macro, proc_macro_attribute, - proc_macro_def_site, proc_macro_derive, proc_macro_expr, proc_macro_gen, @@ -1227,9 +1196,6 @@ symbols! { rust_cold_cc, rust_eh_catch_typeinfo, rust_eh_personality, - rust_eh_register_frames, - rust_eh_unregister_frames, - rust_oom, rustc, rustc_allocator, rustc_allocator_zeroed, @@ -1248,6 +1214,7 @@ symbols! { rustc_deallocator, rustc_def_path, rustc_default_body_unstable, + rustc_deny_explicit_impl, rustc_diagnostic_item, rustc_diagnostic_macros, rustc_dirty, @@ -1302,7 +1269,6 @@ symbols! { rustc_serialize, rustc_skip_array_during_method_dispatch, rustc_specialization_trait, - rustc_stable, rustc_std_internal_symbol, rustc_strict_coherence, rustc_symbol_name, @@ -1430,7 +1396,6 @@ symbols! { static_recursion, staticlib, std, - std_inject, std_panic, std_panic_2015_macro, std_panic_macro, @@ -1444,8 +1409,8 @@ symbols! { str_trim_end, str_trim_start, strict_provenance, + string_deref_patterns, stringify, - stringify_macro, struct_field_attributes, struct_inherit, struct_variant, @@ -1473,10 +1438,8 @@ symbols! { target_has_atomic_load_store, target_os, target_pointer_width, - target_target_vendor, target_thread_local, target_vendor, - task, tbm_target_feature, termination, termination_trait, @@ -1488,7 +1451,6 @@ symbols! { test_removed_feature, test_runner, test_unstable_lint, - then_with, thread, thread_local, thread_local_macro, @@ -1520,13 +1482,13 @@ symbols! { try_trait_v2, tt, tuple, - tuple_from_req, tuple_indexing, tuple_trait, two_phase, ty, type_alias_enum_variants, type_alias_impl_trait, + type_ascribe, type_ascription, type_changing_struct_update, type_id, @@ -1564,7 +1526,6 @@ symbols! { unreachable_2015, unreachable_2015_macro, unreachable_2021, - unreachable_2021_macro, unreachable_code, unreachable_display, unreachable_macro, @@ -1583,7 +1544,6 @@ symbols! { from crates.io via `Cargo.toml` instead?", untagged_unions, unused_imports, - unused_qualifications, unwind, unwind_attributes, unwind_safe_trait, @@ -1917,7 +1877,7 @@ impl Encodable for Symbol { impl Decodable for Symbol { #[inline] default fn decode(d: &mut D) -> Symbol { - Symbol::intern(&d.read_str()) + Symbol::intern(d.read_str()) } } @@ -2092,8 +2052,8 @@ impl Symbol { } impl Ident { - // Returns `true` for reserved identifiers used internally for elided lifetimes, - // unnamed method parameters, crate root module, error recovery etc. + /// Returns `true` for reserved identifiers used internally for elided lifetimes, + /// unnamed method parameters, crate root module, error recovery etc. pub fn is_special(self) -> bool { self.name.is_special() } -- cgit v1.2.3