-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start cleaning up the string interner #34772
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d5f859
Remove unused field `interner` from the parser.
jseyfried f8a934e
Encapsulate `RcStr` in `syntax::util::interner`.
jseyfried 1eb6d0b
Remove `Interner<T>` and rename `StrInterner` to `Interner`.
jseyfried 70e2845
Avoid passing around the thread-local interner in `librustc_metadata`.
jseyfried 752d441
Refactor `get_ident_interner` -> `with_ident_interner`.
jseyfried 060b5c5
Factor the `RefCell` out of the `Interner`.
jseyfried File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -477,17 +477,20 @@ pub type IdentInterner = Interner; | |
// if an interner exists in TLS, return it. Otherwise, prepare a | ||
// fresh one. | ||
// FIXME(eddyb) #8726 This should probably use a thread-local reference. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks obsolete |
||
pub fn get_ident_interner() -> Rc<IdentInterner> { | ||
thread_local!(static KEY: Rc<::parse::token::IdentInterner> = { | ||
Rc::new(mk_fresh_ident_interner()) | ||
pub fn with_ident_interner<T, F: FnOnce(&IdentInterner) -> T>(f: F) -> T { | ||
thread_local!(static KEY: IdentInterner = { | ||
mk_fresh_ident_interner() | ||
}); | ||
KEY.with(|k| k.clone()) | ||
KEY.with(f) | ||
} | ||
|
||
/// Reset the ident interner to its initial state. | ||
pub fn reset_ident_interner() { | ||
let interner = get_ident_interner(); | ||
interner.reset(mk_fresh_ident_interner()); | ||
with_ident_interner(|interner| interner.reset(mk_fresh_ident_interner())); | ||
} | ||
|
||
pub fn clear_ident_interner() { | ||
with_ident_interner(|interner| interner.clear()); | ||
} | ||
|
||
/// Represents a string stored in the thread-local interner. Because the | ||
|
@@ -521,8 +524,7 @@ impl InternedString { | |
|
||
#[inline] | ||
pub fn new_from_name(name: ast::Name) -> InternedString { | ||
let interner = get_ident_interner(); | ||
InternedString::new_from_rc_str(interner.get(name)) | ||
with_ident_interner(|interner| InternedString::new_from_rc_str(interner.get(name))) | ||
} | ||
} | ||
|
||
|
@@ -610,13 +612,13 @@ pub fn intern_and_get_ident(s: &str) -> InternedString { | |
/// Maps a string to its interned representation. | ||
#[inline] | ||
pub fn intern(s: &str) -> ast::Name { | ||
get_ident_interner().intern(s) | ||
with_ident_interner(|interner| interner.intern(s)) | ||
} | ||
|
||
/// gensym's a new usize, using the current interner. | ||
#[inline] | ||
pub fn gensym(s: &str) -> ast::Name { | ||
get_ident_interner().gensym(s) | ||
with_ident_interner(|interner| interner.gensym(s)) | ||
} | ||
|
||
/// Maps a string to an identifier with an empty syntax context. | ||
|
@@ -635,8 +637,7 @@ pub fn gensym_ident(s: &str) -> ast::Ident { | |
// note that this guarantees that str_ptr_eq(ident_to_string(src),interner_get(fresh_name(src))); | ||
// that is, that the new name and the old one are connected to ptr_eq strings. | ||
pub fn fresh_name(src: ast::Ident) -> ast::Name { | ||
let interner = get_ident_interner(); | ||
interner.gensym_copy(src.name) | ||
with_ident_interner(|interner| interner.gensym_copy(src.name)) | ||
// following: debug version. Could work in final except that it's incompatible with | ||
// good error messages and uses of struct names in ambiguous could-be-binding | ||
// locations. Also definitely destroys the guarantee given above about ptr_eq. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is calling
with_ident_interner
even necessary here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is equivalent to
token::intern(&index.to_string())
(except that it avoids a clone).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
token::intern
should be as generic asInterner::intern
?Eventually this could be a generic
Symbol::from
, I guess.I would like it if
with_ident_interner
was actually private.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'd like
token::intern
to be generic but it's a syntax-[breaking-change] sincetoken::intern(&my_string)
wheremy_string: String
wouldn't typecheck (intern(my_string)
orintern(&*my_string)
would, though).I was planning on making
token::intern
generic in a future PR with other interner-related syntax-[breaking-change]s.I agree that
with_ident_interner
should eventually be private and thattoken::intern
should eventually beSymbol::from
(orSymbol::intern
to contrast withSymbol::gensym
).