Repository: dotnet/runtime
File: src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/DependencyAnalysis/MethodCodeNode.cs (C#)

Code before:
                    }
                    sequencePoints[offset] = (sequencePoint.Document, sequencePoint.LineNumber, true);
                }

                // Propagate last known document/line number forward to enable correct mapping when IL offsets decrease at higher native offsets
                for (int i = 1; i < sequencePoints.Length; i++)
                {
                    if (sequencePoints[i].Document == null && sequencePoints[i - 1].Document != null)
                    {
                        sequencePoints[i] = (sequencePoints[i - 1].Document, sequencePoints[i - 1].LineNumber, false);
                    }
                }
            }
            catch (BadImageFormatException)
            {
                // Roslyn had a bug where it was generating bad sequence points:
                // https://github.com/dotnet/roslyn/issues/20118
                // Do not crash the compiler.
                yield break;
            }

            int previousNativeOffset = -1;
            int maxIlOffset = -1;
            foreach (var nativeMapping in _debugLocInfos)
            {
                if (nativeMapping.NativeOffset == previousNativeOffset)
                    continue;

                if (nativeMapping.ILOffset < sequencePoints.Length)
                {
                    var sequencePoint = sequencePoints[nativeMapping.ILOffset];
                    // Emit sequence point if we have it from _debugInfo or if ILOffset decreases.
                    // This handles the case of IL offsets decreasing at higher native offsets.
                    // See WalkILOffsetsCallback in src/coreclr/vm/debugdebugger.cpp for more details.
                    if ((sequencePoint.IsBackedSequencePoint || nativeMapping.ILOffset <= maxIlOffset) &&
                        sequencePoint.Document != null)
                    {
                        yield return new NativeSequencePoint(
                            nativeMapping.NativeOffset,
                            sequencePoint.Document,
                            sequencePoint.LineNumber);
                        previousNativeOffset = nativeMapping.NativeOffset;
                    }
                    maxIlOffset = Math.Max(maxIlOffset, nativeMapping.ILOffset);
                }
            }
        }

        public override int ClassCode => 788492407;

        public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)

Review comment (bug):
Does this code make any assumptions about the order of entries in `_debugLocInfos`, like sorted by native offset, sorted by IL offset, ...?

Code after:
                    }
                    sequencePoints[offset] = (sequencePoint.Document, sequencePoint.LineNumber);
                }

                // Propagate last known document/line number forward to enable correct mapping when IL offsets decrease at higher native offsets
                for (int i = 1; i < sequencePoints.Length; i++)
                {
                    if (sequencePoints[i].Document == null && sequencePoints[i - 1].Document != null)
                    {
                        sequencePoints[i] = (sequencePoints[i - 1].Document, sequencePoints[i - 1].LineNumber);
                    }
                }
            }
            catch (BadImageFormatException)
            {
                // Roslyn had a bug where it was generating bad sequence points:
                // https://github.com/dotnet/roslyn/issues/20118
                // Do not crash the compiler.
                yield break;
            }

            int previousNativeOffset = -1;
            string previousDocument = null;
            int previousLineNumber = -1;
            // OffsetMapping is sorted in order of increasing native offset (but not necessarily by IL offset)
            foreach (var nativeMapping in _debugLocInfos)
            {
                if (nativeMapping.NativeOffset == previousNativeOffset)
                    continue;

                var sequencePoint = sequencePoints[Math.Min(nativeMapping.ILOffset, sequencePoints.Length - 1)];
                // Emit sequence point if its line number or document differ from the previous one.
                // See WalkILOffsetsCallback in src/coreclr/vm/debugdebugger.cpp for more details.
                if (sequencePoint.Document != null && (sequencePoint.Document != previousDocument || sequencePoint.LineNumber != previousLineNumber))
                {
                    yield return new NativeSequencePoint(
                        nativeMapping.NativeOffset,
                        sequencePoint.Document,
                        sequencePoint.LineNumber);
                    previousNativeOffset = nativeMapping.NativeOffset;
                    previousDocument = sequencePoint.Document;
                    previousLineNumber = sequencePoint.LineNumber;
                }
            }
        }

        public override int ClassCode => 788492407;

        public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)
        {
            return comparer.Compare(_method, ((MethodCodeNode)other)._method);