|
25 | 25 | #![recursion_limit = "256"]
|
26 | 26 | // tidy-alphabetical-end
|
27 | 27 |
|
28 |
| -use std::cell::{Cell, Ref, RefCell}; |
| 28 | +use std::cell::{BorrowMutError, Cell, Ref, RefCell, RefMut}; |
29 | 29 | use std::collections::BTreeSet;
|
30 | 30 | use std::fmt::{self, Formatter};
|
31 | 31 | use std::sync::Arc;
|
@@ -598,17 +598,17 @@ struct ModuleData<'ra> {
|
598 | 598 | underscore_disambiguator: CmCell<u32>, // FIXME(batched): Use an atomic in batched import resolution?
|
599 | 599 |
|
600 | 600 | /// Macro invocations that can expand into items in this module.
|
601 |
| - unexpanded_invocations: RefCell<FxHashSet<LocalExpnId>>, |
| 601 | + unexpanded_invocations: CmRefCell<FxHashSet<LocalExpnId>>, |
602 | 602 |
|
603 | 603 | /// Whether `#[no_implicit_prelude]` is active.
|
604 | 604 | no_implicit_prelude: bool,
|
605 | 605 |
|
606 |
| - glob_importers: RefCell<Vec<Import<'ra>>>, |
607 |
| - globs: RefCell<Vec<Import<'ra>>>, |
| 606 | + glob_importers: CmRefCell<Vec<Import<'ra>>>, |
| 607 | + globs: CmRefCell<Vec<Import<'ra>>>, |
608 | 608 |
|
609 | 609 | /// Used to memoize the traits in this module for faster searches through all traits in scope.
|
610 | 610 | traits:
|
611 |
| - RefCell<Option<Box<[(Macros20NormalizedIdent, NameBinding<'ra>, Option<Module<'ra>>)]>>>, |
| 611 | + CmRefCell<Option<Box<[(Macros20NormalizedIdent, NameBinding<'ra>, Option<Module<'ra>>)]>>>, |
612 | 612 |
|
613 | 613 | /// Span of the module itself. Used for error reporting.
|
614 | 614 | span: Span,
|
@@ -660,9 +660,9 @@ impl<'ra> ModuleData<'ra> {
|
660 | 660 | underscore_disambiguator: CmCell::new(0),
|
661 | 661 | unexpanded_invocations: Default::default(),
|
662 | 662 | no_implicit_prelude,
|
663 |
| - glob_importers: RefCell::new(Vec::new()), |
664 |
| - globs: RefCell::new(Vec::new()), |
665 |
| - traits: RefCell::new(None), |
| 663 | + glob_importers: CmRefCell::new(Vec::new()), |
| 664 | + globs: CmRefCell::new(Vec::new()), |
| 665 | + traits: CmRefCell::new(None), |
666 | 666 | span,
|
667 | 667 | expansion,
|
668 | 668 | self_binding,
|
@@ -697,7 +697,7 @@ impl<'ra> Module<'ra> {
|
697 | 697 |
|
698 | 698 | /// This modifies `self` in place. The traits will be stored in `self.traits`.
|
699 | 699 | fn ensure_traits<'tcx>(self, resolver: &impl AsRef<Resolver<'ra, 'tcx>>) {
|
700 |
| - let mut traits = self.traits.borrow_mut(); |
| 700 | + let mut traits = self.traits.borrow_mut_unchecked(); |
701 | 701 | if traits.is_none() {
|
702 | 702 | let mut collected_traits = Vec::new();
|
703 | 703 | self.for_each_child(resolver, |r, name, ns, binding| {
|
@@ -2587,7 +2587,7 @@ impl<T: Copy> CmCell<T> {
|
2587 | 2587 | self.value.get()
|
2588 | 2588 | }
|
2589 | 2589 |
|
2590 |
| - pub fn update_unchecked(&self, f: impl FnOnce(T) -> T) |
| 2590 | + fn update_unchecked(&self, f: impl FnOnce(T) -> T) |
2591 | 2591 | where
|
2592 | 2592 | T: Copy,
|
2593 | 2593 | {
|
@@ -2618,3 +2618,54 @@ impl<T> CmCell<T> {
|
2618 | 2618 | self.value.into_inner()
|
2619 | 2619 | }
|
2620 | 2620 | }
|
| 2621 | +#[derive(Default)] |
| 2622 | +struct CmRefCell<T> { |
| 2623 | + ref_cell: RefCell<T>, |
| 2624 | +} |
| 2625 | + |
| 2626 | +impl<T> CmRefCell<T> { |
| 2627 | + const fn new(value: T) -> CmRefCell<T> { |
| 2628 | + CmRefCell { ref_cell: RefCell::new(value) } |
| 2629 | + } |
| 2630 | + |
| 2631 | + #[inline] |
| 2632 | + #[track_caller] |
| 2633 | + #[allow(dead_code)] |
| 2634 | + fn borrow_mut<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> RefMut<'_, T> { |
| 2635 | + if r.assert_speculative { |
| 2636 | + panic!("Not allowed to mutably borrow a CmRefCell during speculative resolution"); |
| 2637 | + } |
| 2638 | + self.ref_cell.borrow_mut() |
| 2639 | + } |
| 2640 | + |
| 2641 | + #[inline] |
| 2642 | + #[track_caller] |
| 2643 | + fn borrow_mut_unchecked(&self) -> RefMut<'_, T> { |
| 2644 | + self.ref_cell.borrow_mut() |
| 2645 | + } |
| 2646 | + |
| 2647 | + #[inline] |
| 2648 | + #[track_caller] |
| 2649 | + fn try_borrow_mut_unchecked(&self) -> Result<RefMut<'_, T>, BorrowMutError> { |
| 2650 | + self.ref_cell.try_borrow_mut() |
| 2651 | + } |
| 2652 | + |
| 2653 | + #[inline] |
| 2654 | + #[track_caller] |
| 2655 | + #[allow(dead_code)] |
| 2656 | + fn try_borrow_mut<'ra, 'tcx>( |
| 2657 | + &self, |
| 2658 | + r: Resolver<'ra, 'tcx>, |
| 2659 | + ) -> Result<RefMut<'_, T>, BorrowMutError> { |
| 2660 | + if r.assert_speculative { |
| 2661 | + panic!("Not allowed to mutably borrow a CmRefCell during speculative resolution"); |
| 2662 | + } |
| 2663 | + self.ref_cell.try_borrow_mut() |
| 2664 | + } |
| 2665 | + |
| 2666 | + #[inline] |
| 2667 | + #[track_caller] |
| 2668 | + fn borrow(&self) -> Ref<'_, T> { |
| 2669 | + self.ref_cell.borrow() |
| 2670 | + } |
| 2671 | +} |
0 commit comments