Correctly associate own-line comments in bodies (#4671)

This commit is contained in:
Micha Reiser 2023-06-01 08:12:53 +02:00 committed by GitHub
parent 46c3b3af94
commit be31d71849
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 1747 additions and 144 deletions

View file

@ -1,7 +1,6 @@
use ruff_python_ast::node::AnyNodeRef;
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::ptr::NonNull;
/// Used as key into the [`MultiMap`] storing the comments per node by [`Comments`].
///
@ -22,86 +21,6 @@ impl<'a> NodeRefEqualityKey<'a> {
pub(super) fn node(&self) -> AnyNodeRef {
self.node
}
fn ptr(self) -> NonNull<()> {
match self.node {
AnyNodeRef::ModModule(node) => NonNull::from(node).cast(),
AnyNodeRef::ModInteractive(node) => NonNull::from(node).cast(),
AnyNodeRef::ModExpression(node) => NonNull::from(node).cast(),
AnyNodeRef::ModFunctionType(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtFunctionDef(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtAsyncFunctionDef(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtClassDef(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtReturn(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtDelete(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtAssign(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtAugAssign(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtAnnAssign(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtFor(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtAsyncFor(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtWhile(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtIf(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtWith(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtAsyncWith(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtMatch(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtRaise(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtTry(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtTryStar(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtAssert(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtImport(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtImportFrom(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtGlobal(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtNonlocal(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtExpr(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtPass(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtBreak(node) => NonNull::from(node).cast(),
AnyNodeRef::StmtContinue(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprBoolOp(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprNamedExpr(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprBinOp(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprUnaryOp(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprLambda(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprIfExp(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprDict(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprSet(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprListComp(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprSetComp(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprDictComp(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprGeneratorExp(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprAwait(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprYield(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprYieldFrom(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprCompare(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprCall(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprFormattedValue(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprJoinedStr(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprConstant(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprAttribute(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprSubscript(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprStarred(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprName(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprList(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprTuple(node) => NonNull::from(node).cast(),
AnyNodeRef::ExprSlice(node) => NonNull::from(node).cast(),
AnyNodeRef::ExcepthandlerExceptHandler(node) => NonNull::from(node).cast(),
AnyNodeRef::PatternMatchValue(node) => NonNull::from(node).cast(),
AnyNodeRef::PatternMatchSingleton(node) => NonNull::from(node).cast(),
AnyNodeRef::PatternMatchSequence(node) => NonNull::from(node).cast(),
AnyNodeRef::PatternMatchMapping(node) => NonNull::from(node).cast(),
AnyNodeRef::PatternMatchClass(node) => NonNull::from(node).cast(),
AnyNodeRef::PatternMatchStar(node) => NonNull::from(node).cast(),
AnyNodeRef::PatternMatchAs(node) => NonNull::from(node).cast(),
AnyNodeRef::PatternMatchOr(node) => NonNull::from(node).cast(),
AnyNodeRef::TypeIgnoreTypeIgnore(node) => NonNull::from(node).cast(),
AnyNodeRef::Comprehension(node) => NonNull::from(node).cast(),
AnyNodeRef::Arguments(node) => NonNull::from(node).cast(),
AnyNodeRef::Arg(node) => NonNull::from(node).cast(),
AnyNodeRef::Keyword(node) => NonNull::from(node).cast(),
AnyNodeRef::Alias(node) => NonNull::from(node).cast(),
AnyNodeRef::Withitem(node) => NonNull::from(node).cast(),
AnyNodeRef::MatchCase(node) => NonNull::from(node).cast(),
}
}
}
impl Debug for NodeRefEqualityKey<'_> {
@ -112,7 +31,7 @@ impl Debug for NodeRefEqualityKey<'_> {
impl PartialEq for NodeRefEqualityKey<'_> {
fn eq(&self, other: &Self) -> bool {
self.ptr().eq(&other.ptr())
self.node.ptr_eq(other.node)
}
}
@ -120,7 +39,7 @@ impl Eq for NodeRefEqualityKey<'_> {}
impl Hash for NodeRefEqualityKey<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.ptr().hash(state);
self.node.as_ptr().hash(state);
}
}